Android 内容提供者 ContentProvider

ContentProvider 是android的四大组件之一,用来为程序提供数据(可用于不同程序之间)

第一步:

在此之前需要先用UriMatcher对Uri进行匹配处理:

1
2
3
4
5
6
7
private static finalintPETS=100;
private static finalintPET_ID=101;
private static UriMatcheruriMatcher=newUriMatcher(UriMatcher.NO_MATCH);
static{
uriMatcher.addURI("com.example.android.pets","pets",PETS);
uriMatcher.addURI("com.example.android.pets","pets/#",PET_ID);
}

然后再 ContentProvider 增删查改中对应匹配的方法进行操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Int rCode=uriMatcher.match(uri);
SQLiteDatabasedb=mPetDbHelper.getReadableDatabase();
Cursor cursor;
switch(rCode){
casePETS:
cursor=db.query(PetsContract.PetsEntry.TABLE_NAME,strings,s,strings1,null,null,null);
break;
casePET_ID:
Stringselection=PetsContract.PetsEntry._ID+"=?";
String[]selectionArgs=newString[]{String.valueOf(ContentUris.parseId(uri))};
cursor=db.query(PetsContract.PetsEntry.TABLE_NAME,strings,selection,selectionArgs,null,null,null);
break;
default:
Throw newIllegalArgumentException("Uriisinvalid.");
}

第二步:

在方法中调用数据库操作方法来操作数据库.
ContentUris.withAppendedId(uri,id) 可以将 uri 与 id 组合起来
第三步:
在查询的尾部要通知更新数据:
cursor.setNotificationUri(getContext().getContentResolver(),uri);
其他的地方用下面的语句来通知
getContext().getContentResolver().notifyChange(uri,null);
第四步:
编写其 XML 文件
代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public class PetProvider extends ContentProvider {
PetDbHelper mPetDbHelper;
private static final int PETS = 100;
private static final int PET_ID = 101;
private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
uriMatcher.addURI("com.example.android.pets","pets",PETS);
uriMatcher.addURI("com.example.android.pets","pets/#",PET_ID);
}
@Override
public boolean onCreate() {
mPetDbHelper = new PetDbHelper(getContext());
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
int rCode = uriMatcher.match(uri);
SQLiteDatabase db = mPetDbHelper.getReadableDatabase();
Cursor cursor;
switch (rCode){
case PETS:
cursor = db.query(PetsContract.PetsEntry.TABLE_NAME,strings ,s,strings1,null,null,null);
break;
case PET_ID:
String selection = PetsContract.PetsEntry._ID + " = ? ";
String[] selectionArgs = new String [] {String.valueOf(ContentUris.parseId(uri))};
cursor = db.query(PetsContract.PetsEntry.TABLE_NAME,strings ,selection,selectionArgs,null,null,null);
break;
default:
throw new IllegalArgumentException("Uri is invalid.");
}
cursor.setNotificationUri(getContext().getContentResolver(),uri);
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}

@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = uriMatcher.match(uri);
switch (match) {
case PETS:
return insertPet(uri, contentValues);
default:
throw new IllegalArgumentException("Insertion is not supported for " + uri);
}
}

private Uri insertPet(Uri uri, ContentValues values) {
SQLiteDatabase db = mPetDbHelper.getWritableDatabase();
long id = db.insert(PetsContract.PetsEntry.TABLE_NAME,null,values);
getContext().getContentResolver().notifyChange(uri,null);
return ContentUris.withAppendedId(uri, id);
}

@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase database = mPetDbHelper.getWritableDatabase();

final int match = uriMatcher.match(uri);
switch (match) {
case PETS:
getContext().getContentResolver().notifyChange(uri,null);
return database.delete(PetsContract.PetsEntry.TABLE_NAME, selection, selectionArgs);
case PET_ID:
selection = PetsContract.PetsEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
getContext().getContentResolver().notifyChange(uri,null);
return database.delete(PetsContract.PetsEntry.TABLE_NAME, selection, selectionArgs);
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
}
private int updatePet(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db=mPetDbHelper.getWritableDatabase();
return db.update(PetsContract.PetsEntry.TABLE_NAME,values,selection,selectionArgs);
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
final int match = uriMatcher.match(uri);
switch (match) {
case PETS:
getContext().getContentResolver().notifyChange(uri,null);
return updatePet(uri, contentValues, s, strings);
case PET_ID:
s = PetsContract.PetsEntry._ID + "=?";
strings = new String[] { String.valueOf(ContentUris.parseId(uri)) };
int i=updatePet(uri, contentValues, s, strings);
getContext().getContentResolver().notifyChange(uri,null);
return i;
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
}

参考地址:
https://github.com/udacity/ud845-Pets/blob/a244b5a4cbda9c5fb7b551032b40166542da9869/app/src/main/java/com/example/android/pets/data/PetProvider.java