浅析ContentProvide

来源:互联网 发布:windows10装机必备软件 编辑:程序博客网 时间:2024/06/05 21:16

一、作用。

    提供私有数据供别的APP使用。
二、主要步骤。
   1、定义ContentProvider的子类继承ContentProvider类。
    2、重写相关方法。 其方法有
          1)booleanonCreate():一般在里面获得SQLiteDatabase实例,
             SQLiteDatabase db=newDBHelp(getContext()).getWritableDatabase();
          2)Cursor query(Uri uri, String[] projection, Stringselection,String[] selectionArgs, StringsortOrder) :用于书写查询数据的操作。参数1:uri   参数2:要查找的列   参数4:查找条件    参数5:条件中占位符的值   参数6:排序
         3)Uri insert(Uri uri,ContentValues values) :用于书写插入数据的操作。
         4)int update(Uri uri,ContentValues values, String selection,String[] selectionArgs):用于书写更新数据的操作。
         5)int delete(Uri uri, String selection, String[]selectionArgs):用于书写删除数据的操作。
         6)String getType(Uriuri):根据传入的uri判断数据的数量,多条数据将type置为:
"vnd.android.cursor.dir/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;
          单条数据将type置为:vnd.android.cursor.item/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;
   3、在清单注册文件注册:application节点内部---》provider:name、authorities(域名)、exported(是否要暴露数据),也可设置权限:
     读:readpermission-->值字符串A
          发布权限:permission节点:name:值:值字符串A---》application节点的外面
      写:writepermission-->值字符串A
          发布权限:permission节点:name:值:值字符串A---》application节点的外面
三、匹配uri
   1、获取UriMatcher 对象: UriMatcher matcher=newUriMatcher(UriMatcher.NO_MATCH);
   2、添加要哦匹配的URI,将其写在静态代码块中。  
          方法: matcher.addURI(authority,path,code);
          参数1:authority  参数2:资源路径:表名  参数3:该uri匹配成功的返回码code
         完整路径Uri:
                matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME, 1);
         模糊匹配:id--->#数值
         matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME+"/#", 2);
          模糊匹配:name---》*文本
                matcher.addURI(AUTHORITY,DBHelper.TABLE_NAME+"/*",3); 
   3、拼接uri及获取uri里的参数
          ContentUris.withAppendedId(contentUri,Long.parseLong(id))--->ContentUris.parseId()

         Uri.withAppendedPath(contentUri,name)--->uri.getLastPathSegment();

 

ContentResolver对象调用的query() 、insert() 、update()、delete() 就是 ContentProvider类中的重写后的query() 、insert() 、update()、delete() 方法。
3.定义ContentProvider的Uri,
4. UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
静态代码块  static{
//添加要 匹配的URI
<span style="white-space:pre"></span>参数1:authority   
<span style="white-space:pre"></span>参数2:资源路径:表名      
<span style="white-space:pre"></span>参数3:该uri匹配成功的返回码code//完整路径Uri:content://com.example.day17demo01provider.db.StuProvider/javaScore 
<span style="white-space:pre"></span>matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME, 1);//模糊匹配:id--->#数值
<span style="white-space:pre"></span>matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME+"/#", 2);//模糊匹配:name---》*文本matcher.addURI(AUTHORITY, DBHelper.TABLE_NAME+"/*", 3);
 
}
//实例化ContentProvider的时候调用---》不要做耗时操作@Overridepublic boolean onCreate() {db=new DBHelper(getContext()).getWritableDatabase();return false;}

//查找数据--->查找SQLite---》SQLiteDataBase//参数1:uri    参数2:要查找的列    参数4:查找条件     参数5:条件中占位符的值   参数6:排序@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {//把传进来的uri进行匹配---》定位一条数据还是多条数据int code = matcher.match(uri);Cursor cursor = null;switch (code) {case 1://全部cursor = db.query(DBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);break;case 2://指定id//取出uri中的idlong id = ContentUris.parseId(uri);cursor = db.query(DBHelper.TABLE_NAME, projection, DBHelper.JAVA_ID+" = ?", new String[]{id+""}, null, null, sortOrder);break;case 3://指定name//取出name值String name=uri.getLastPathSegment();//查找条件---》name的值--》模糊匹配cursor=db.query(DBHelper.TABLE_NAME, projection, " name like '%"+name+"%'", null, null, null, sortOrder);break;default:break;}return cursor;}

//获取类型--->name、没有值匹配---》1、3---》多条数据//2:单条数据@Overridepublic String getType(Uri uri) {String type=null;int code = matcher.match(uri);switch (code) {case 1://多条数据case 3:type="vnd.android.cursor.dir/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;break;case 2:type="vnd.android.cursor.item/vnd."+AUTHORITY+"."+DBHelper.TABLE_NAME;break;default:break;}return type;}//插入数据@Overridepublic Uri insert(Uri uri, ContentValues values) {int code = matcher.match(uri);Uri withAppendedId =null;if(code==1){long id = db.insert(DBHelper.TABLE_NAME, null, values);withAppendedId = ContentUris.withAppendedId(uri, id);}return withAppendedId;}//删除@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {int rawNum =0;int code = matcher.match(uri);if(code==2){//包含有数值--》idlong id = ContentUris.parseId(uri);//删除条件//数据库的删除操作rawNum = db.delete(DBHelper.TABLE_NAME, DBHelper.JAVA_ID+" = ?", new String[]{id+""});}return rawNum;}//更新@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {int code = matcher.match(uri);switch (code) {case 1://没有指定name、iddb.update(DBHelper.TABLE_NAME, values, selection, selectionArgs);break;case 2://id//取出idlong id = ContentUris.parseId(uri);//根据id去更新数db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_ID+" = ?", new String[]{id+""});break;case 3://name//取出nameString name = uri.getLastPathSegment();//根据name去更新db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_NAME+" like '%"+name+"%'", null);break;default:break;}return 0;}}
0 0
原创粉丝点击