一、继承ContentProvider类,并重写该类用于提供数据和存储数据的方法;二、外部访问通过ContentResolver去访问并操作这些被暴露的数据。
使用ContentProvider共享数据的好处是统一了数据访问方式。
ContentProvider使用前需要注册:
<manifest .... > <application android:icon="@drawable/icon" android:label="@string/app_name"> <provider android:name=".PersonContentProvider" android:authorities="com.faith.providers.personprovider" /> </application> </manifest> UriMatcher类使用介绍: 因为Uri代表了要操作的数据,所以我们经常需要解析Uri,并从Uri中获取数据。 Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。 UriMatcher类用于匹配Uri,它的用法如下: 首先第一步把你需要匹配Uri路径全部给注册上,如下: [java] view plain copy print? //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码 UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); //如果match()方法匹配content://com.faith.providers.personprovider/person路径,返回匹配码为1 sMatcher.addURI(“com.faith.providers.personprovider”, “person”, 1);//添加需要匹配uri,如果匹配就会返回匹配码 //如果match()方法匹配content://com.faith.providers.personprovider/person/230路径,返回匹配码为2 sMatcher.addURI(“com.faith.providers.personprovider”, “person/#”, 2);//#号为通配符 switch (sMatcher.match(Uri.parse("content://com.faith.providers.personprovider/person/10"))) { case 1 break; case 2 break; default://不匹配 break; } 注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://com.faith.providers.personprovider/person路径,返回的匹配码为1 ContentUris类使用介绍: ContentUris类用于获取Uri路径后面的ID部分,它有两个比较实用的方法: [java] view plain copy print? withAppendedId(uri, id)用于为路径加上ID部分: Uri uri = Uri.parse("content://com.faith.providers.personprovider/person") Uri resultUri = ContentUris.withAppendedId(uri, 10); //生成后的Uri为:content://com.faith.providers.personprovider/person/10 parseId(uri)方法用于从路径中获取ID部分: Uri uri = Uri.parse("content://com.faith.providers.personprovider/person/10") long personid = ContentUris.parseId(uri);//获取的结果为:10 实现代码: [java] view plain copy print? /** * 这个继承ContentProvider的类,其主要用到的类有UriMatcher,ContentUris */ public class PersonContentProvider extends ContentProvider { private final static String TABLE_NAME = "person"; private final static String TABLE_COLUMN_ID = "id"; private final static String TABLE_COLUMN_NAME = "name"; private final static String AUTHORITY = "com.faith.providers.personprovider"; private final static String PERSONS_PATH = "person"; private final static String PERSON_PATH = "person/#";//#表示任意的数字 private final static int PERSONS = 1; private final static int PERSON = 2; private final static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH); static{ //UriMatcher类的一个方法 sMatcher.addURI(AUTHORITY, PERSONS_PATH, PERSONS); sMatcher.addURI(AUTHORITY, PERSON_PATH, PERSON); } private DbOpenHelper mHelper = null; @Override public boolean onCreate() { mHelper = new DbOpenHelper(this.getContext()); return true; } /* * 查询数据库中的信息 */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase database = mHelper.getWritableDatabase(); //UriMatcher中的一个重要方法,判断两个Uri是否相匹配 switch(sMatcher.match(uri)){ case PERSONS: /* * 当Uri为“content://com.faith.providers.personprovider/person”时,触发这个模块 * 是指查询person表中所有的数据信息(当然条件是selection) */ return database.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); case PERSON: /* * 当Uri为“content://com.faith.providers.personprovider/person/#”时,触发这个模块 * 是指查询当id = #(所代表的数字)时的那一条数据的具体信息 * 需要添加一个条件(在原先的selection条件的基础上添加) */ long id = ContentUris.parseId(uri); String where = TABLE_COLUMN_ID + "=" + id; if(selection != null && !"".equals(selection)){ where = where + " and " + selection; } return database.query(TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder); default : //均不匹配的时候,抛出异常 throw new IllegalArgumentException("Unknown Uri : " + uri); } } /* * 向数据库中插入一条信息 */ @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase database = mHelper.getWritableDatabase(); //由于此时的values中具体是否含有数据是不确定的,所以此时需要在第二个参数中添加person表中的非主键的一列 long id = database.insert(TABLE_NAME, TABLE_COLUMN_NAME, values); return ContentUris.withAppendedId(uri, id); } /* * 删除数据库中的信息 */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { //此方法同query方法 SQLiteDatabase database = mHelper.getWritableDatabase(); switch(sMatcher.match(uri)){ case PERSONS: return database.delete(TABLE_NAME, selection, selectionArgs); case PERSON: long id = ContentUris.parseId(uri); String where = TABLE_COLUMN_ID + "=" + id; if(selection != null && !"".equals(selection)){ where = where + " and " + selection; } return database.delete(TABLE_NAME, where, selectionArgs); default: throw new IllegalArgumentException("Unknown Uri : " + uri); } } /* *更新数据库中的信息 */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { //此方法同query方法 SQLiteDatabase database = mHelper.getWritableDatabase(); switch(sMatcher.match(uri)){ case PERSONS: return database.update(TABLE_NAME, values, selection, selectionArgs); case PERSON: long id = ContentUris.parseId(uri); String where = TABLE_COLUMN_ID + "=" + id; if(selection != null && !"".equals(selection)){ where = where + " and " + selection; } return database.update(TABLE_NAME, values, where, selectionArgs); default: throw new IllegalArgumentException("Unknown Uri : " + uri); } } /* * 取得Uri所对应的值的类型,是集合型或者非集合型 * 集合型需要在其返回值前添加“vnd.android.cursor.dir/” * 非集合型需要在其返回值前添加"vnd.android.cursor.item/" */ @Override public String getType(Uri uri) { switch(sMatcher.match(uri)){ case PERSONS: //集合类型,返回值前面一般是固定的,后面的值是自己添加的,也可以加上包路径 return "vnd.android.cursor.dir/" + TABLE_NAME; case PERSON: //非集合类型数据,返回值前面一般是固定的,后面的值是自己添加的,也可以加上包路径 return "vnd.android.cursor.item/" + TABLE_NAME; default: throw new IllegalArgumentException("Unknown Uri : " + uri); } } } 别忘了在Manifest.xml文件中注册这个ContentProvider. 在其他应用中的单元测试类方法: [java] view plain copy print? public class AccessContentProvider extends AndroidTestCase { private final static String TAG = "AccessContentProvider"; public void testSave() throws Throwable { ContentResolver resolver = this.getContext().getContentResolver(); Uri insertUri = Uri.parse("content://com.faith.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "meijun"); values.put("age", "15"); values.put("phone", "199893"); Uri uri = resolver.insert(insertUri, values); Log.d(TAG, uri.toString()); } public void testUpdate() throws Throwable { ContentResolver resolver = this.getContext().getContentResolver(); Uri updateUri = Uri.parse("content://com.faith.providers.personprovider/person/6"); ContentValues values = new ContentValues(); values.put("name", "meijun"); values.put("age", "35"); values.put("phone", "1998933243"); resolver.update(updateUri, values, null, null); } public void testQuery() throws Throwable { ContentResolver resolver = this.getContext().getContentResolver(); Uri queryUri = Uri.parse("content://com.faith.providers.personprovider/person"); Cursor cursor = resolver.query(queryUri, null, null, null, null); while(cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); short age = cursor.getShort(cursor.getColumnIndex("age")); Log.d(TAG, "id = " + id + ",name = " + name + ",phone = " + phone + ",age = " + age); } } public void testDelete() throws Throwable { ContentResolver resolver = this.getContext().getContentResolver(); Uri deleteUri = Uri.parse("content://com.faith.providers.personprovider/person/6"); resolver.delete(deleteUri, null, null); } public void testType() throws Throwable { ContentResolver resolver = this.getContext().getContentResolver(); Uri typeUri = Uri.parse("content://com.faith.providers.personprovider/person/6"); String type = resolver.getType(typeUri); Log.d(TAG, type); } }