contentprovider用法

一、清单注册

<provider android:name="cn.itcast.db.PersonProvider" 

android:authorities="cn.itcast.providers.personprovider"

android:exported=true/> 前面类名,后面主机名,唯一识别

UriMatcher类使用介绍:

因为Uri代表了要操作的数据,所以我们经常需要解析Uri,并从Uri中获取数据。

Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。
UriMatcher类用于匹配Uri,它的用法如下:
首先第一步把你需要匹配Uri路径全部给注册上,如下:

  1. //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码  
  2. UriMatcher  sMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
  3. //如果match()方法匹配content://com.faith.providers.personprovider/person路径,返回匹配码为1  
  4. sMatcher.addURI(“com.faith.providers.personprovider”, “person”, 1);//添加需要匹配uri,如果匹配就会返回匹配码  
  5. //如果match()方法匹配content://com.faith.providers.personprovider/person/230路径,返回匹配码为2  
  6. sMatcher.addURI(“com.faith.providers.personprovider”, “person/#”, 2);//#号为通配符  
  7. switch (sMatcher.match(Uri.parse("content://com.faith.providers.personprovider/person/10"))) {   
  8.    case 1  
  9.     break;  
  10.    case 2  
  11.     break;  
  12.    default://不匹配  
  13.     break;  
  14. }  

    注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://com.faith.providers.personprovider/person路径,返回的匹配码为1

ContentUris类使用介绍:

ContentUris类用于获取Uri路径后面的ID部分,它有两个比较实用的方法:

  1. withAppendedId(uri, id)用于为路径加上ID部分:  
  2. Uri uri = Uri.parse("content://com.faith.providers.personprovider/person")  
  3. Uri resultUri = ContentUris.withAppendedId(uri, 10);   
  4. //生成后的Uri为:content://com.faith.providers.personprovider/person/10  
  5.   
  6. parseId(uri)方法用于从路径中获取ID部分:  
  7. Uri uri = Uri.parse("content://com.faith.providers.personprovider/person/10")  
  8. long personid = ContentUris.parseId(uri);//获取的结果为:10  

二、在应用程序子包下创建一个ContentProvider类

public class PersonProvider extends ContentProvider {
	
	private DBOpenHelper dbOpenHelper;
	private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int PERSONS = 1;
	private static final int PERSON = 2;
	//添加要匹配的uri,分两种,一个是整张表,一个是某一行
	static{
			MATCHER.addURI("cn.itcast.providers.personprovider", "person", PERSONS);	
			MATCHER.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);//#代表数字
	}
	
	//获得数据库操作实例
	public boolean onCreate() {
	    dbOpenHelper = new DBOpenHelper(this.getContext());
		return true;}
   //添加操作
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		switch (MATCHER.match(uri)) {
		case 1:
			long rowid = db.insert("person", "name", values);
			Uri insertUri = ContentUris.withAppendedId(uri, rowid);//cn.itcast.providers.personprovider/person/rowid
			this.getContext().getContentResolver().notifyChange(insertUri, null);//发出数据变化通知
			return insertUri;
		default:
			throw new IllegalArgumentException("this is unknown Uri" + uri);
			
		}
		
	}
	//删除操作
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int num = 0;
		switch (MATCHER.match(uri)) {
		case 1:
			num = db.delete("person", selection, selectionArgs);
			break;
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection!=null &&!"".equals(selection.trim())){
				where += "and" + selection;
			}
			num = db.delete("person", where, selectionArgs);
			break;
			
		default:
			throw new IllegalArgumentException("this is unknown Uri:" + uri);
		}
			return num;
	}


	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int num = 0;
		switch (MATCHER.match(uri)) {
		case 1:
			num = db.update("person", values, selection, selectionArgs);
			break;
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection!=null &&!"".equals(selection.trim())){
				where += "and" + selection;
			}		
			num = db.update("person", values, where, selectionArgs);
			break;
			
		default:
			throw new IllegalArgumentException("this is unknown Uri:" + uri);
		}
			return num;
	}


@Override//外部应用查询内容提供者数据
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
	
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		switch (MATCHER.match(uri)) {
		case 1:
			return db.query("person", null, selection, selectionArgs, null, null, sortOrder);
	
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection!=null &&!"".equals(selection.trim())){
				where += "and" + selection;
			}
		
			return db.query("person", null, where, selectionArgs, null, null, sortOrder);
			
		default:
			throw new IllegalArgumentException("this is unknown Uri:" + uri);
		}
			
	}

	public String getType(Uri uri) {
		
		switch (MATCHER.match(uri)) {
		case 1:
			return "vnd.android.crsor.dir/person";
		case 2:	
			return "vnd.android.crsor.item/person";
		default:
			throw new IllegalArgumentException("this is unknown Uri:" + uri);
		}
		
	}}

相关测试方法

  1. public class AccessContentProvider extends AndroidTestCase {  
  2.     private final static String TAG = "AccessContentProvider";  
  3.   
  4.     public void testSave() throws Throwable {  
  5.         ContentResolver resolver = this.getContext().getContentResolver();  
  6.         Uri insertUri = Uri.parse("content://com.faith.providers.personprovider/person");  
  7.         ContentValues values = new ContentValues();  
  8.         values.put("name""meijun");  
  9.         values.put("age""15");  
  10.         values.put("phone""199893");  
  11.         Uri uri = resolver.insert(insertUri, values);  
  12.         Log.d(TAG, uri.toString());  
  13.     }  
  14.       
  15.     public void testUpdate() throws Throwable {  
  16.         ContentResolver resolver = this.getContext().getContentResolver();  
  17.         Uri updateUri = Uri.parse("content://com.faith.providers.personprovider/person/6");  
  18.         ContentValues values = new ContentValues();  
  19.         values.put("name""meijun");  
  20.         values.put("age""35");  
  21.         values.put("phone""1998933243");  
  22.         resolver.update(updateUri, values, nullnull);  
  23.     }  
  24.       
  25.     public void testQuery() throws Throwable {  
  26.         ContentResolver resolver = this.getContext().getContentResolver();  
  27.         Uri queryUri = Uri.parse("content://com.faith.providers.personprovider/person");  
  28.         Cursor cursor = resolver.query(queryUri, nullnullnullnull);  
  29.         while(cursor.moveToNext()){  
  30.             int id = cursor.getInt(cursor.getColumnIndex("id"));  
  31.             String name = cursor.getString(cursor.getColumnIndex("name"));  
  32.             String phone = cursor.getString(cursor.getColumnIndex("phone"));  
  33.             short age = cursor.getShort(cursor.getColumnIndex("age"));  
  34.             Log.d(TAG, "id = " + id + ",name = " + name   
  35.                     + ",phone = " + phone + ",age = " + age);  
  36.         }  
  37.     }  
  38.       
  39.     public void testDelete() throws Throwable {  
  40.         ContentResolver resolver = this.getContext().getContentResolver();  
  41.         Uri deleteUri = Uri.parse("content://com.faith.providers.personprovider/person/6");  
  42.         resolver.delete(deleteUri, nullnull);  
  43.     }  
  44.       
  45.     public void testType() throws Throwable {  
  46.         ContentResolver resolver = this.getContext().getContentResolver();  
  47.         Uri typeUri = Uri.parse("content://com.faith.providers.personprovider/person/6");  
  48.         String type = resolver.getType(typeUri);  
  49.         Log.d(TAG, type);  
  50.     }  
  51. }  

http://blog.csdn.net/faith_boys/article/details/8917395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值