浅析ContentProvide

一、作用。

    提供私有数据供别的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的时候调用---》不要做耗时操作
	@Override
	public boolean onCreate() {
		db=new DBHelper(getContext()).getWritableDatabase();
		return false;
	}

//查找数据--->查找SQLite---》SQLiteDataBase
	//参数1:uri    参数2:要查找的列    参数4:查找条件     参数5:条件中占位符的值   参数6:排序
	@Override
	public 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中的id
			long 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:单条数据
	@Override
	public 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;
	}
	//插入数据
	@Override
	public 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;
	}
	//删除
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int rawNum =0;
		
		int code = matcher.match(uri);
		
		if(code==2){//包含有数值--》id
			long id = ContentUris.parseId(uri);//删除条件
			
			//数据库的删除操作
			rawNum = db.delete(DBHelper.TABLE_NAME, DBHelper.JAVA_ID+" = ?", new String[]{id+""});
		}
		
		return rawNum;
	}
	//更新
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		
		int code = matcher.match(uri);
		switch (code) {
		case 1://没有指定name、id
			db.update(DBHelper.TABLE_NAME, values, selection, selectionArgs);
			break;
		case 2://id
			//取出id
			long id = ContentUris.parseId(uri);
			
			//根据id去更新数
			db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_ID+" = ?", new String[]{id+""});
			break;
		case 3://name
			//取出name
			String name = uri.getLastPathSegment();
			//根据name去更新
			db.update(DBHelper.TABLE_NAME, values, DBHelper.JAVA_NAME+" like '%"+name+"%'", null);
			
			break;


		default:
			break;
		}
		
		return 0;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值