1.content providers資料共用原理:
當一個應用需要公開共用它的資料時,可以通過建立自己的content providers然後通過Manifest把content providers註冊到android系統,然後其他應用通過contentresolver訪問content providers的uri來操作指定的content providers,實際上,contentresolver就是相當於content providers的一個代理,其它應用程式通過contentresolver訪問共用的content providers。
content providers是單例對象,既它只建立一次,後面對它的訪問都是同一個執行個體
2.建立content providers,實現抽象類別ContentProvider
//實現建立的時候介面
public abstract boolean onCreate();
當content providers第一次被訪問,執行個體化時候它會被調用,以後的訪問oncreate方法將不再被調用。
//實現查詢的時候介面
public abstract Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder);
uri表示資料content providers的位置路徑,也可以直接使用id定位到一條記錄上去,projection表示查詢指定的列名數組,selection表示查詢條件,用"and"分割多個查詢條件,當查詢條件中存在查詢參數"?"時,需要使用selectionArgs添加查詢參數,sortOrder用來指定查詢順序
//實現擷取資料類型介面
public abstract String getType(Uri uri);
meta資料,擷取資料類型
//實現插入資料介面
public abstract Uri insert(Uri uri, ContentValues values);
values需要插入的內容,uri指定content providers
//實現刪除資料介面
public abstract int delete(Uri uri, String selection, String[] selectionArgs);
//實現更新資料介面
public abstract int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs);
3.使用ContentResolver訪問content providers
//擷取ContentResolver對象
ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
//插入資料
ContentValues values = new ContentValues();
values.put(NotePad.Notes.TITLE, "title1");
values.put(NotePad.Notes.NOTE, "NOTENOTE1");
contentResolver.insert(NotePad.Notes.CONTENT_URI, values);
//刪除資料
ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
//指定id刪除
contentResolver.delete(ContentUris.withAppendedId(NotePad.Notes.CONTENT_URI,
2), null, null);
//使用where條件刪除
contentResolver.delete(NotePad.Notes.CONTENT_URI,
NotePad.Notes.TITLE+"=/"title1/"", null);
//使用where條件參數刪除
contentResolver.delete(NotePad.Notes.CONTENT_URI, NotePad.Notes.TITLE + "=?", new String[] { "title2" });
//修改資料
ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put(NotePad.Notes.TITLE, "title1");
values.put(NotePad.Notes.NOTE, "NOTENOTE2");
contentResolver.update(NotePad.Notes.CONTENT_URI, values, NotePad.Notes.TITLE + "=?", new String[] { "title1" });
//查詢資料
ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
String columns[] = new String[] { NotePad.Notes._ID, NotePad.Notes.TITLE, NotePad.Notes.NOTE, NotePad.Notes.CREATEDDATE, NotePad.Notes.MODIFIEDDATE };
Cursor cur = contentResolver.query( NotePad.Notes.CONTENT_URI, columns, NotePad.Notes.TITLE + "=?", new String[] { "title1" }, NotePad.Notes._ID);
if (cur.moveToFirst()) {
String id = null;
String title = null;
String note = null;
do {
id = cur.getString(cur.getColumnIndex(NotePad.Notes._ID));
title = cur.getString(cur.getColumnIndex(NotePad.Notes.TITLE));
note = cur.getString(cur.getColumnIndex(NotePad.Notes.NOTE));
System.out.println("Record:"+id+" "+title+" "+note+" ");
} while (cur.moveToNext());
}