Content Provider:一個組件,必須放在應用的主包或應用的子包之下;
組件的配置需要在清單文件中進行配置;content provider需要在application節點中進行配置;
內容提供者在應用中的作用是對外共享數據(任意類型的數據)使用的,別的程序可以對數據進行CRUD,如通訊錄;
如果采用文件的方式對外共享數據,會因為文件的類型不同而需要使用不同的api訪問方式導致訪問繁雜,而內容提供者提供了統一的api對數據進行操作;
<provider
android:name=".PersonProvider"<!-- 內容提供者類的名稱 -->
android:authorities="cn.wordtech.providers.personprovider"
android:exported="false" ><!-- 解決 android Permission Denial error!,在監聽內容提供者數據發生變化時需要配置此項 -->
</provider>
另:
android:authorities:為內容提供者指定一個唯一的標識,這樣別的應用才可以唯一獲取此provider;
Uri 代表了要操作的數據;
Uri主要包含兩部分的信息:1>>需要操作的ContentProvider,2>>對ContentProvider中的什么數據進行操作
ContentProvider(內容提供者)的scheme已經由Android所規定,scheme為:content://
主機名(或Authority)用于唯一標識這個ContentProvider,外部調用者可以根據此標識來找到它,
路徑(path)可以用來表示我們要操作的數據,路徑的構建根據業務而定。
ex:
要操作person表中id為10的記錄,可以構建這樣的路徑:/person/10
要操作person表中id為10的記錄的name字段,可以構建這樣的路徑:/person/10/name
要操作person表中的所有記錄,可以構建這樣的路徑:/person
要操作XXX表中的記錄,可以構建這樣的路徑:/XXX
要操作的數據不一定是數據庫中的文件,也可以是文件,xml或網絡等其它方式
ex:
要操作xml文件中person節點下的name節點,可以構建這樣的路徑:/person/name
// 可以供外部的應用查詢數據,返回查詢得到的游標對象
@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 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);
}
}
}
新聞熱點
疑難解答
圖片精選