當應用需要通過ContentProvider對外共享數據時,第一步需要繼承ContentProvider並重寫下面方法:
public class PersonContentProvider extends ContentProvider{
public boolean onCreate()//該方法在ContentProvider創建後就會被調用, Android開機後, ContentProvider在其它應用第一次訪問它時纔會被創建。
public Uri insert(Uri uri, ContentValues values)//該方法用於供外部應用往ContentProvider添加數據。
public int delete(Uri uri, String selection, String[] selectionArgs)//該方法用於供外部應用從ContentProvider刪除數據。
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)//該方法用於供外部應用更新ContentProvider中的數據。
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)//該方法用於供外部應用從ContentProvider中獲取數據。
public String getType(Uri uri)}
該方法用於返回當前Url所代表數據的MIME類型。如果操作的數據屬於集合類型,那麼MIME類型字符串應該以vnd.android.cursor.dir/開頭,例如:要得到所有person記錄的Uri爲
content://cn.itcast.provider.personprovider/person,那麼返回的MIME類型字符串應該爲:“vnd.android.cursor.dir/person”。如果要操作的數據屬於非集合類型數據,那麼MIME類型字符串應該以vnd.android.cursor.item/開頭,例如:得到id爲10的person記錄,Uri爲
content://cn.itcast.provider.personprovider/person/10,那麼返回的MIME類型字符串應該爲:“vnd.android.cursor.item/person”。
第二步需要在AndroidManifest.xml使用<provider>對該ContentProvider進行配置,爲了能讓其他應用找到該ContentProvider , ContentProvider 採用了authorities(主機名/域名)對它進行唯一標識,你可以把 ContentProvider看作是一個網站(想想,網站也是提供數據者),authorities 就是他的域名:
<manifest .... >
<application android:icon="@drawable/icon" android:label="@string/app_name">
<provider android:name=".PersonContentProvider" android:authorities="cn.itcast.providers.personprovider"/>
</application>
</manifest>
注意:一旦應用繼承了ContentProvider類,後面我們就會把這個應用稱爲ContentProvider(內容提供者)。
package cn.itcast.db;
import cn.itcast.service.DBOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PersonContentProvider extends ContentProvider {
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
private DBOpenHelper dbOpenHelper;
static{
matcher.addURI("cn.itcast.providers.personprovider", "person", PERSONS);
matcher.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);
}
@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(this.getContext());
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0 ;//已經刪除的記錄數量
switch (matcher.match(uri)) {
case PERSONS:
num = db.delete("person", selection, selectionArgs);
break;
case PERSON:long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
where = where + " and "+ selection;
}
num = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return num;
}
@Override
public String getType(Uri uri) {//返回當前操作的數據類型
switch (matcher.match(uri)) {
case PERSONS://操作的是集合類型數據
return "vnd.android.cursor.dir/person";
case PERSON:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
long id = 0 ;
switch (matcher.match(uri)) {
case PERSONS:
id = db.insert("person", "personid", values);//得到記錄的id
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);//返回代表新增記錄的Uri
case PERSON:
id = db.insert("person", "personid", values);//得到記錄的id
String strUri = uri.toString();
Uri personUri = Uri.parse(strUri.substring(0, strUri.lastIndexOf("/")));
getContext().getContentResolver().notifyChange(personUri, null);
return ContentUris.withAppendedId(personUri, id);
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (matcher.match(uri)) {
case PERSONS:
return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
where = where + " and "+ selection;
}
return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0 ;//已經修改的記錄數量
switch (matcher.match(uri)) {
case PERSONS:
num = db.update("person", values, selection, selectionArgs);
break;
case PERSON:long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = where + " and "+ selection;
}
num = db.update("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
getContext().getContentResolver().notifyChange(uri, null);//通知數據發生變化
return num;
}
}