標籤:des android style blog http color io os ar
Content Resolver介紹:
開發人員文檔中這麼定義的:
This class provides applications access to the content model.
這個類為應用提供訪問Content模型的功能。
Content Resolver是我們應用裡單一全域執行個體,為我們訪問我們自己的應用或其他應用的Content Provider。就如同名字所描述的:Content Resolver接收來自客戶的請求,然後解決它們的請求,通過將請求指向特定主機名稱的Content Provider解決。
Content Resolver包括了CRUD方法(create,read,update,delete)。這些正好與Content Provider中的抽象方法
(insert ,query,update,delete)一 一對應。
(圖轉自http://www.cnblogs.com/plokmju/p/android_ContentProvider.html)
Content Resolver不知道Content Provider是怎樣對資料操作的,也不需要知道。Content Resolver的每個方法通過傳遞
URI到特定的Content Provider來實現對資料產生影響的操作。
下面我將通過一個Content Resolver的Demo實現對Android系統的User Dictionary進行增刪改查操作。
增加(insert):
//調用getContentResolver()擷取ContentResolver對象ContentResolver contentResolver = getContentResolver;ContentValues values = new ContentValues();values.put(Words.WORD,"NewWord"); //調用ContentResolver.insert(Uri url, ContentValues values)方法增加資料contentResolver.insert(UserDictionary.Words.CONTENT_URI, values);
刪除(delete):
//調用ContentResolver.delete(Uri url, String where, String[] selectionArgs)//方法刪除資料,返回的是刪除的行數long deleted = cr.delete(Words.CONTENT_URI,Words.WORD + "= ?", new String[]{"NewWord"});
尋找(query):
String [] projection = new String[]{ BaseColumns._ID, UserDictionary.Words.WORD };Cursor cursor =cr .query(UserDictionary.Words.CONTENT_URI, projection, null, null, null); if(cursor.moveToFirst()){ do{ long id = cursor.getLong(0); String word= cursor.getString(1); Map<String,Object>map = new HashMap<String,Object>(); map.put("id", id); map.put("word", word); list.add(map); }while(cursor.moveToNext());
修改(update):
//調用ContentResolver.update(Uri uri, ContentValues values, String where, //String[] selectionArgs)方法更新資料//返回更新行數的個數long update = cr.update(uri, values, null, null);
應用: 使用者字典:
本人郵箱:JohnTsai.Work@gmail.com,歡迎交流討論。
歡迎轉載,轉載請註明網址:http://www.cnblogs.com/JohnTsai
Android四大組件之——ContentProvider(二)