一. 瞭解 ContentProvider java
1. 什麼是ContentProvider android
讓開發者在多個應用中操做數據,如存儲,修改刪除 的惟一方式 ,一個ContentProvider 實現 了下面的接口。
數據庫
ContentProvider.insert( Uri ,ContentValues ) ContentProvider.query (Uri ,String [] ,String ,String [], String ) ContentProvider.update(Uri .ConentValues .String ,String []); ContentProvider.delete( Uri ,String .String ); ContentProvider.getType (Uri );
經過 這些接口,咱們不用關心數據 的結構 。ide
2. 什麼是URIspa
UUniversal Resources Identifier,在安卓 中,URI有三個部分code
(1) "content://" ,開頭xml
(2) 數據路徑對象
(3) ID ,可選 ,若是 不寫,全部 的數據 。接口
content://contacts/peopel開發
不少經常使用 的URI安卓 已經定義也常量 。
3.ContentResolver
ContentProvider將數據暴露給外面,而後咱們用ContentResolve獲得數據 。至關 因而一個數據 的消費者,咱們用 getContentResolver來獲得當前 應用的ContentResolver對象 。
與ContentProvider一一對應,它有五個接口。
它們將以Cursor的形式返回結果 ,與數據 庫相同。
二. 使用ContentProvider
系統的一些 程序 ,如聯繫人,通話記錄等,每每做爲 ContentProvider向外提供 數據 ,咱們能夠用managedQuery()方法很方便查詢相關數據
1.聯繫人
三個步驟,咱們在這裏將Activity extends ListActivity,重點突出ContentProvider的做用。
(1) 查詢聯繫人,獲得Cursor對象
managedQuery( Uri uri ,String [] projection ,String selection ,String [] selectionArgs ,String sortOrder)
projection: 要查詢的數據 的屬性。
(2)新建 一個Adapter
ListAdapter adapter = new SimpleCursorAdapter ( Context context ,int layout ,Cursor c ,String [] from ,int [] to );
(3) 設置Adapter
setListAdapter (adapter );
例子:
Cursor c = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,null); ListAdapter adapter = new SimpleCursorAdapter (getApplicationContext(), android.R.layout.simple_list_item_2, c, new String [] {ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, new int [] {android.R.id.text1,android.R.id.text2}); setListAdapter (adapter);
在ContactsContract中,咱們能夠找到全部Contacts的信息。
最後要注意,讀取聯繫人時,要的權限 。
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
2. 通話記錄
和上面的相比 ,要改的是
Uri :CallLog.Calls.CONTENT_URI
還有兩個屬性: 號碼,通話時間
以下:
Cursor c = managedQuery(CallLog.Calls.CONTENT_URI, null,null,null,null); ListAdapter adapter = new SimpleCursorAdapter (getApplicationContext(), android.R.layout.simple_list_item_2, c, new String [] {Calls.NUMBER,Calls.DURATION}, new int [] {android.R.id.text1,android.R.id.text2}); setListAdapter (adapter);
3. 多媒體信息
4. 書籤
三 .使用ContentResolver
外部應用須要對ContentProvider中的數據進行添加、刪除、修改和查詢操做時,可使用ContentResolver 類來完成,要獲取ContentResolver 對象,可使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,來操做數據。
1. 刪除 數據
首先,咱們要獲得ContentResolver 對象
ContentResolver resolver = getContentResolver();
使用delete
ContentResolver.delete (Uri uri ,String where ,String [] selectionArgs );
如要刪除 全部 的聯繫人:
resolver.delete( Data.CONTENT_URI ,null ,null );
若是要刪除 名字爲WES的,以下
resolver.delete (Data.CONTENT_URI ,StructuredName.DISPLAY_NAME +"=", new String [] {"WES"});
2. 查詢數據
與數據庫相似
ContentResolver.query (Uri uri ,String [] projection ,String selection ,String [] SelectionArgs ,String sortOrder );
projection :要查詢的屬性。
如要查詢全部人l,的信息。
resolver.query (ContactsContract.CommenDataKinds.Phone,CONTENT_URI , null ,null,null, null);
3. 更新數據
ContentResolver.update (Uri uri ,ContentValues values ,String where ,String [] selectionArgs );
以下例子:
values.put(StructruedName.DISPLAY_NAME ,"WES" ); resolver.update(Data.CONTENT_URI , values , StruacturedName.DISPLAY_NAME+ "=?", new String [] {"WES"});
4. 插入數據
這個 看起來很容易 ,可是實際 上很難。
ContentResolver .insert( Uri uri ,ContentValues values );
暫時不寫