public class PersonProvider extends ContentProvider {// Content Provider需要继承自ContentProvider类
// 删改查中,都有两种情况:
// person 对整个表进行操作
// person/id 对表中的与id对应记录进行操作
private DBOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);// new UriMatcher(code);code即为匹配不成功时返回的值;
private static final int PERSONS = 1;
private static final int PERSON = 2;
// 设置匹配项
static {
MATCHER.addURI("cn.wordtech.providers.personprovider", "person",PERSONS);
MATCHER.addURI("cn.wordtech.providers.personprovider", "person/#",PERSON);// #号表示数字
}
// content://cn.wordtech.providers.personprovider/person
@Override
public boolean onCreate() {
// 由系统调用,当ContentProvider的实例被创建出来的时候被调用,Android开机后,当第一次有应用访问ContentProvider时才创建ContentProvider;
dbOpenHelper = new DBOpenHelper(getContext(), 1);
return false;
}
// 可以供外部的应用查询数据,返回查询得到的游标对象
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case 1:
return db.query("person", projection, selection, selectionArgs,
null, null, sortOrder);
case 2:
long rowid = ContentUris.parseId(uri);// 返回要操作的id
String where = "personid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where += "and" + selection;
}
return db.query("person", projection, where, selectionArgs, null,
null, sortOrder);
default:
throw new IllegalArgumentException("");
}
}
// 此方法用于返回目前Uri所代表的数据的MIME类型,
// 如果操作的数据属于集合类型,则MIME字符串就以"vnd.android.cursor.dir"开头
// 如果操作的数据属于非集合类型,则MIME字符串就以"vnd.android.cursor.item"开头
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case 1:
return "vnd.android.cursor.dir/person";
case 2:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("");
}
}
// 此方法需要返回操作记录对应的Uri
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case 1:
long rowid = db.insert("person", "", values);// 返回行号?主键值
// Uri insertUri = Uri
// .parse("content://com.sqlite.PersonProvider/person/"
// + rowid);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;
default:
throw new IllegalArgumentException("this is Unknow Uri:" + uri);
}
}
// 返回受影响的行数
@Override
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);// 返回要操作的id
String where = "personid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where += "and" + selection;
}
num = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("");
}
return num;
}
@Override // 返回受影响的行数
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);// 返回要操作的id
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("");
}
return num;
}
}
public class AccessContentProviderTest extends AndroidTestCase {
public void testinsert() {
Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
ContentResolver cr = this.getContext().getContentResolver(); // This class provides applications access to the content model
ContentValues values = new ContentValues();
values.put("name", "Livingstone");
values.put("phone", "110");
values.put("amount", "1111111111");
cr.insert(uri, values);// 在cr的内部会调用内容提供者的值;
}
public void testdelete() {
Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person/1");// 根据标识名得到内容提供者
ContentResolver cr = this.getContext().getContentResolver();
cr.delete(uri, null, null);
}
public void testupdate() {
Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person/2");// 根据标识名得到内容提供者
ContentResolver cr = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "Livingstone11");
cr.update(uri, values, null, null);
}
public void testquery() {
Uri uri = Uri.parse("content://cn.wordtech.providers.personprovider/person");// 根据标识名得到内容提供者
ContentResolver cr = this.getContext().getContentResolver();
Cursor cursor = cr.query(uri, null, null, null, "personid asc");
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i("Name", name);
}
}
}