前言
- ContentProvider 是 android 提供的專門用于不同應(yīng)用之間進(jìn)行數(shù)據(jù)共享的方式鼻疮。其內(nèi)部也是通過 Binder 機(jī)制實(shí)現(xiàn)。
- 系統(tǒng)預(yù)設(shè)了許多 ContentProvider,如通訊錄 日程表信息砾嫉。要進(jìn)行跨進(jìn)程訪問這些信息杉编,只需通過 ContentResolver 的 query update insert 和 delete方法(都運(yùn)行在 Binder 線程池中)即可。
- ContentProvider 主要以表格的形式來組織數(shù)據(jù)赌厅,并可以包含多個(gè)表穷绵。除表格,ContentProvider 還支持文件數(shù)據(jù),如圖片特愿,視頻等仲墨。
- ContentProvider(內(nèi)容提供者)的scheme已經(jīng)由Android所規(guī)定為:content://
方法說明
/**
* 第一個(gè)參數(shù)是要查詢的provider的Uri。
* 第二個(gè)參數(shù)是要返回的數(shù)據(jù)列的名字揍障,即想要返回表的哪些列目养。如果返回全部列,就置為null毒嫡“┮希可以理解為對(duì)列的過濾。
* 第三個(gè)參數(shù)可以理解為對(duì)行的過濾兜畸。相當(dāng)于sql的where語(yǔ)句努释,如果返回全部行,置為null咬摇。
* 第四個(gè)參數(shù)伐蒂,selection argment。
* 第五個(gè)參數(shù)菲嘴,對(duì)返回行的排序饿自。舉例說明
**/
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {...}
如:
查詢sim卡中含有某個(gè)字的模糊匹配
public ArrayList<Contact_Object> getPhoneNumber(String name, Context context) {
ArrayList<Contact_Object> arrayList = new ArrayList<Contact_Object>();
String ret = null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'" + name +"%'";
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, null, null);
while (cur.moveToNext())
{
Contact_Object contactObject = new Contact_Object();
contactObject.name = cur.getString(0);
contactObject.mobileNumber = cur.getString(1);
contactObject.type = cur.getInt(2);
contactObject.uri = cur.getString(3);
arrayList.add(contactObject);
}
cur.close();
return arrayList;
}
示例
- 聲明自定義的 ContentProvider
public class BookProvider extends ContentProvider {
//ContentProvider唯一標(biāo)識(shí)
public static final String AUTHORITY = "com.ryg.chapter_2.book.provider";
public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"+ AUTHORITY + "/book");
public static final Uri USER_CONTENT_URI = Uri.parse("content://"+ AUTHORITY + "/user");
public static final int BOOK_URI_CODE = 0;
public static final int USER_URI_CODE = 1;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
//public void addURI(String authority, String path, int code)
sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
}
private Context mContext;
private SQLiteDatabase mDb;
@Override
public boolean onCreate() { //main thread
mContext = getContext();
initProviderData();
return true;
}
//初始化放入數(shù)據(jù)
private void initProviderData() {
mDb = new DbOpenHelper(mContext).getWritableDatabase();
mDb.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
mDb.execSQL("delete from " + DbOpenHelper.USER_TALBE_NAME);
mDb.execSQL("insert into book values(3,'Android');");
mDb.execSQL("insert into book values(4,'Ios');");
mDb.execSQL("insert into book values(5,'Html5');");
mDb.execSQL("insert into user values(1,'jake',1);");
mDb.execSQL("insert into user values(2,'jasmine',0);");
}
//運(yùn)行在Binder線程池中,可能每次運(yùn)行的線程不一樣龄坪。
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Log.d(TAG, "query, current thread:" + Thread.currentThread().getName());
String table = getTableName(uri);
if (table == null) {throw new IllegalArgumentException("Unsupported URI: " + uri);}
return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
}
@Override
public String getType(Uri uri) {return null;}
@Override
public Uri insert(Uri uri, ContentValues values) {
String table = getTableName(uri);
if (table == null) { throw new IllegalArgumentException("Unsupported URI: " + uri); }
mDb.insert(table, null, values);
//通知外界當(dāng)前的 ContentProvider 中數(shù)據(jù)發(fā)生了變化
mContext.getContentResolver().notifyChange(uri, null);
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
String table = getTableName(uri);
if (table == null) { throw new IllegalArgumentException("Unsupported URI: " + uri); }
int count = mDb.delete(table, selection, selectionArgs);
//通知外界當(dāng)前的 ContentProvider 中數(shù)據(jù)發(fā)生了變化
if (count > 0) { getContext().getContentResolver().notifyChange(uri, null); }
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
String table = getTableName(uri);
if (table == null) { throw new IllegalArgumentException("Unsupported URI: " + uri); }
int row = mDb.update(table, values, selection, selectionArgs);
//通知外界當(dāng)前的 ContentProvider 中數(shù)據(jù)發(fā)生了變化
if (row > 0) { getContext().getContentResolver().notifyChange(uri, null); }
return row;
}
private String getTableName(Uri uri) {
String tableName = null;
switch (sUriMatcher.match(uri)) {
case BOOK_URI_CODE:
tableName = DbOpenHelper.BOOK_TABLE_NAME;
break;
case USER_URI_CODE:
tableName = DbOpenHelper.USER_TALBE_NAME;
break;
}
return tableName;
}
}
- 數(shù)據(jù)庫(kù)幫助類 DbOpenHelper
public class DbOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "book_provider.db";
public static final String BOOK_TABLE_NAME = "book";
public static final String USER_TALBE_NAME = "user";
private static final int DB_VERSION = 3;
private String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS "
+ BOOK_TABLE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT)";
private String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS "
+ USER_TALBE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT," + "sex INT)";
public DbOpenHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); }
@Override
public void onCreate(SQLiteDatabase db) { //如果創(chuàng)建了昭雌,就不會(huì)回調(diào)這個(gè)方法。
db.execSQL(CREATE_BOOK_TABLE);
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO ignored
}
}
- 使用
public class ProviderActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provider);
// Uri uri = Uri.parse("content://com.ryg.chapter_2.book.provider");
// getContentResolver().query(uri, null, null, null, null);
// getContentResolver().query(uri, null, null, null, null);
//查詢book表
Uri bookUri = Uri.parse("content://com.ryg.chapter_2.book.provider/book");
ContentValues values = new ContentValues();
values.put("_id", 6);
values.put("name", "程序設(shè)計(jì)的藝術(shù)");
getContentResolver().insert(bookUri, values);
Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
while (bookCursor.moveToNext()) {
Book book = new Book();
book.bookId = bookCursor.getInt(0);
book.bookName = bookCursor.getString(1);
}
bookCursor.close();
//查詢User表
Uri userUri = Uri.parse("content://com.ryg.chapter_2.book.provider/user");
Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
while (userCursor.moveToNext()) {
User user = new User();
user.userId = userCursor.getInt(0);
user.userName = userCursor.getString(1);
user.isMale = userCursor.getInt(2) == 1;
}
userCursor.close();
}
}