内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据的共享功能,是android的四大组件之一。内容提供器的用法一般有两种,一种是使用向右的内容提供器来读取和操作相应程序中的数据;另一种是创建自己的内容提供其给自己程序要共享出去的数据提供外部访问接口。
Cursor cursor=null;
try {
cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(name+"\n"+number);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(cursor!=null)
cursor.close();
}
<provider android:authorities="com.example.tlh.databasetest.provider" android:name="com.example.tlh.databasetest.MyProvider" android:exported = "true">
</provider>
public class MyProvider extends ContentProvider {
private MyDatabaseHelper dbHelper;
public static final int TABLE1_DIR=0;
public static final int TABLE1_ITEM=1;
public static final int TABLE2_DIR=2;
public static final int TABLE2_ITEM=3;
public static final String AUTHORITY="com.example.tlh.databasetest.provider";
private static UriMatcher uriMatcher;
static {
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",TABLE1_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",TABLE1_ITEM);
uriMatcher.addURI(AUTHORITY,"category",TABLE2_DIR);
uriMatcher.addURI(AUTHORITY,"category/#",TABLE2_ITEM);
//当后面调用uriMatcher.match()时返回相应自定义常量。
}
@Override
public boolean onCreate() {//当且仅当有外部程序的ContentResolver尝试访问我们的程序数据时,初始化内容提供器,会调用此方法,若成功则return true.
//得到数据库帮助类实例便于以后调用数据库的CURD操作方法。
dbHelper=new MyDatabaseHelper(getContext(),"BookStore.db");
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=null;
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
cursor=db.query("Book",projection,null,null,null,null,sortOrder);
break;
case TABLE1_ITEM:
String bookID=uri.getPathSegments().get(1);
cursor=db.query("Book",projection,"id=?",new String[]{"bookID"},null,null,sortOrder);
break;
case TABLE2_DIR:
cursor=db.query("Category",projection,null,null,null,null,sortOrder);
break;
case TABLE2_ITEM:
String categoryID=uri.getPathSegments().get(1);
cursor=db.query("Category",projection,"id=?",new String[]{"categoryID"},null,null,sortOrder);
break;
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {//调用ContentResolver的CURD方法传递过来的URI参数
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book";
case TABLE1_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book";
case TABLE2_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category";
case TABLE2_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.category";
}
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
Uri uriReturn=null;
switch(uriMatcher.match(uri)){
case TABLE1_DIR:
case TABLE1_ITEM:
Long bookID=db.insert("Book",null,values);
uriReturn=Uri.parse("content://"+AUTHORITY+"/Book/"+bookID);
break;
case TABLE2_DIR:
case TABLE2_ITEM:
Long categoryID=db.insert("Category",null,values);
uriReturn=Uri.parse("content://"+AUTHORITY+"/Category/"+categoryID);
break;
}
return uriReturn;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
int deleteLine=0;
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
deleteLine=db.delete("Book",selection,selectionArgs);
break;
case TABLE1_ITEM:
String bookID=uri.getPathSegments().get(1);
deleteLine=db.delete("Book","id=?",new String[]{bookID});
break;
case TABLE2_DIR:
deleteLine=db.delete("Category",selection,selectionArgs);
break;
case TABLE2_ITEM:
String categoryID=uri.getPathSegments().get(1);
deleteLine=db.delete("Category","id=?",new String[]{categoryID});
break;
}
return deleteLine;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
int updateLine=0;
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
updateLine=db.update("Book",values,selection,selectionArgs);
break;
case TABLE1_ITEM:
String bookID=uri.getPathSegments().get(1);
updateLine=db.delete("Book","id=?",new String[]{bookID});
break;
case TABLE2_DIR:
updateLine=db.delete("Category",selection,selectionArgs);
break;
case TABLE2_ITEM:
String categoryID=uri.getPathSegments().get(1);
updateLine=db.delete("Category","id=?",new String[]{categoryID});
break;
}
return updateLine;
}
}