前言
- ContentProvider 是 android 提供的专门用于不同应用之间进行数据共享的方式。其内部也是通过 Binder 机制实现。
- 系统预设了许多 ContentProvider,如通讯录 日程表信息。要进行跨进程访问这些信息,只需通过 ContentResolver 的 query update insert 和 delete方法(都运行在 Binder 线程池中)即可。
- ContentProvider 主要以表格的形式来组织数据,并可以包含多个表。除表格,ContentProvider 还支持文件数据,如图片,视频等。
- ContentProvider(内容提供者)的scheme已经由Android所规定为:content://
方法说明
/**
* 第一个参数是要查询的provider的Uri。
* 第二个参数是要返回的数据列的名字,即想要返回表的哪些列。如果返回全部列,就置为null。可以理解为对列的过滤。
* 第三个参数可以理解为对行的过滤。相当于sql的where语句,如果返回全部行,置为null。
* 第四个参数,selection argment。
* 第五个参数,对返回行的排序。举例说明
**/
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {...}
如:
查询sim卡中含有某个字的模糊匹配
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唯一标识
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;
}
//初始化放入数据
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);");
}
//运行在Binder线程池中,可能每次运行的线程不一样。
@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);
//通知外界当前的 ContentProvider 中数据发生了变化
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);
//通知外界当前的 ContentProvider 中数据发生了变化
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);
//通知外界当前的 ContentProvider 中数据发生了变化
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;
}
}
- 数据库帮助类 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) { //如果创建了,就不会回调这个方法。
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", "程序设计的艺术");
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();
}
}