自定义ContentProvider

一、作用。
      提供私有数据供别的APP使用。
二、主要步骤。
       1、定义 ContentProvider的子类继承 ContentProvider类。
     2、重写相关方法。  其方法有
            1)boolean onCreate():一般在里面获得SQLiteDatabase实例,
                SQLiteDatabase  db=new DBHelp(getContext()).getWritableDatabase();
            2) Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) :用于书写查询数据的操作。 参数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(Uri uri):根据传入的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=new UriMatcher(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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值