**PersonContentProvider类:**
public class PersonContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.itheima.sqlitedemo.providers.PersonContentProvider";
private static final int PRESON_INSERT_CODE = 0;
private static final int PERSON_DELETE_CODE = 1;
private static final int PERSON_UPDATE_CODE = 2;
private static final int PERSON_QUERY_ALL_CODE = 3;
private static final int PERSON_QUERY_ITEM_CODE = 4;
private static UriMatcher uriMatcher;
private PersonSQLiteOpenHelper mOpenHelper;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "person/insert", PRESON_INSERT_CODE);
uriMatcher.addURI(AUTHORITY, "person/delete", PERSON_DELETE_CODE);
uriMatcher.addURI(AUTHORITY, "person/update", PERSON_UPDATE_CODE);
uriMatcher.addURI(AUTHORITY, "person/queryAll", PERSON_QUERY_ALL_CODE);
uriMatcher.addURI(AUTHORITY, "person/query/#", PERSON_QUERY_ITEM_CODE);
}
@Override
public boolean onCreate() {
mOpenHelper = new PersonSQLiteOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
switch (uriMatcher.match(uri)) {
case PERSON_QUERY_ALL_CODE:
if(db.isOpen()) {
Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
break;
case PERSON_QUERY_ITEM_CODE:
if(db.isOpen()) {
long id = ContentUris.parseId(uri);
Cursor cursor = db.query("person", projection, "_id = ?", new String[]{id + ""}, null, null, sortOrder);
return cursor;
}
break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return null;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case PERSON_QUERY_ALL_CODE:
return "vnd.android.cursor.dir/person";
case PERSON_QUERY_ITEM_CODE:
return "vnd.android.cursor.item/person";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
switch (uriMatcher.match(uri)) {
case PRESON_INSERT_CODE:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
long id = db.insert("person", null, values);
db.close();
return ContentUris.withAppendedId(uri, id);
}
break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case PERSON_DELETE_CODE:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
int count = db.delete("person", selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (uriMatcher.match(uri)) {
case PERSON_UPDATE_CODE:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(db.isOpen()) {
int count = db.update("person", values, selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("uri不匹配: " + uri);
}
return 0;
}
**利用内容提供者:**
public class TextCase extends AndroidTestCase {
private static final String TAG = "TextCase";
public void testInsert() {
Uri uri = Uri.parse("content://com.itheima.sqlitedemo.providers.PersonContentProvider/person/insert");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "fengjie");
values.put("age", 90);
uri = resolver.insert(uri, values);
Log.i(TAG, "uri: " + uri);
long id = ContentUris.parseId(uri);
Log.i(TAG, "添加到: " + id);
}
public void testDelete() {
Uri uri = Uri.parse("content://com.itheima.sqlitedemo.providers.PersonContentProvider/person/delete");
ContentResolver resolver = getContext().getContentResolver();
String where = "_id = ?";
String[] selectionArgs = {"21"};
int count = resolver.delete(uri, where, selectionArgs);
Log.i(TAG, "删除行: " + count);
}
public void testUpdate() {
Uri uri = Uri.parse("content://com.itheima.sqlitedemo.providers.PersonContentProvider/person/update");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "lisi");
int count = resolver.update(uri, values, "_id = ?", new String[]{"20"});
Log.i(TAG, "更新行: " + count);
}
public void testQueryAll() {
Uri uri = Uri.parse("content://com.itheima.sqlitedemo.providers.PersonContentProvider/person/queryAll");
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, "_id desc");
if(cursor != null && cursor.getCount() > 0) {
int id;
String name;
int age;
while(cursor.moveToNext()) {
id = cursor.getInt(0);
name = cursor.getString(1);
age = cursor.getInt(2);
Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);
}
cursor.close();
}
}
public void testQuerySingleItem() {
Uri uri = Uri.parse("content://com.itheima.sqlitedemo.providers.PersonContentProvider/person/query/#");
uri = ContentUris.withAppendedId(uri, 20);
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, null);
if(cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
cursor.close();
Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);
}
}