public class Myprovider extends ContentProvider { //定义一个UriMatcher器 private static final UriMatcher Myurimatcher=new UriMatcher(UriMatcher.NO_MATCH); private static final int QUERYSECUSS = 0; private MyopenHelper myopenHelper; static{ //定义静态代码块用来添加匹配规则 /*authority:和在清单文件中自定义的必须一致 Myurimatcher.addURI("com.lianxi.myprovider", "query", QUERYSECUSS); } @Override public boolean onCreate() { myopenHelper = new MyopenHelper(getContext()); return false; } public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int code=Myurimatcher.match(uri); if (code==QUERYSECUSS) {//判断调用者传过来的uri和自己定义是否匹配 //若匹配成功把query方法实现 SQLiteDatabase sDatabase=myopenHelper.getReadableDatabase(); Cursor cursor=sDatabase.query("info", projection, selection, selectionArgs, null, null, sortOrder); return cursor; }else{ throw new IllegalArgumentException("路径不匹配,请检查路径是否正确"); } }
这是自己定义的内容提供者!
Mainactivity代码如下:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Uri uri=Uri.parse("content://com.lianxi.myprovider/query"); Cursor cursor =getContentResolver().query(uri, null, null, null, null); if (cursor!=null && cursor.getCount()>0) { while (cursor.moveToNext()) { String name=cursor.getString(1); String moneny=cursor.getString(2); System.out.println("内容解析者查询结果:name"+name+"--------moneny"+moneny); } }
运行工程报出如下日志:
08-05 20:39:50.901: E/AndroidRuntime(828): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lianxi.contenreciver/com.lianxi.contenreciver.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.lianxi.createdatebase.Myprovider from ProcessRecord{41985148 828:com.lianxi.contenreciver/u0a10047} (pid=828, uid=10047) that is not exported from uid 10046
求大神指教这是怎么回事。。!