一、清单注册
<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路径全部给注册上,如下:
- //常量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部分,它有两个比较实用的方法:
- 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
二、在应用程序子包下创建一个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);
}
}}
相关测试方法
- 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);
- }
- }
http://blog.csdn.net/faith_boys/article/details/8917395