contentProvider,contentprovider執行個體
Android一共有四大組件:activity、contentProvider、Intent、service》》》
下面介紹一下ContentProvider:
內容提供者將一些特定的應用程式資料供給其它應用程式使用。資料可以儲存於檔案系統、SQLite資料庫或其它方式。內容提供者繼承於ContentProvider 基類,為其它應用程式取用和儲存它管理的資料實現了一套標準方法。然而,應用程式並不直接調用這些方法,而是使用一個 ContentResolver 對象,調用它的方法作為替代。ContentResolver可以與任意內容提供者進行會話,與其合作來對所有相關互動通訊進行管理。
1、contentProvider可以讓不同程式之間的資料進行交換,它把不同應用程式的資料庫操作標準統一起來,並且將這個標準暴露給其他應用程式。
2、ContentProvider的操作方法
a) Boolean onCreate() //啟動組件時調用
b) Int delete(Uri uri,String selection,Sting [ ] selectionArgs)
// 根據指定的uri刪除資料,並返回刪除的資料行數
c) Finnal Context getContext()
// 返回context對象
d) String getType(Uril uril)
// 根據指定的uri,返回操作的MIME類型
e)
f) Uril insert(Uri uri,ContextVaules,values )
// 根據指定的uri進行增加資料的操作,並且返回增加後的uri,在此uri中會附帶新的資料的id
g) Cursor query(Uri uri, String [ ]projection,String selection,String[ ]selectionArgs,String sortOrder )
//根據指定的uri執行查詢操作,所有的查詢結果通過cursor對象返回
h) Int update (Uri uri,ContentValues Values,String selection,string[]selectionArgs)
// 根據指定的uri進行資料的更新操作,並且返回更新資料的行數
3、Uri類常用的方法:
String decode(String s) // 對字串進行編碼
String encode(String s) //對編碼後的字串進行解碼
Uri fromFile(File file) // 從指定的檔案之中讀取uri
Uri withAppendedPath(Uri baseUri,String pathSegment)
// 在uri後面加參數
Uri parse(String uriString) // 將給出的字串地址變為uri對象
4、Uri 解釋:
content://hup.lyl.contentProvider/mytable/3
// Content://包名(一般情況下)/資料表/id
Content: // 協議
Authority(包名): // 外部調用者可以根據這個標識找到它。要用小寫字母。
Path(mytable/3):
// content://Authority/Path 表的全部資料
// content://Authority/Path/id id的資料
// content://Authority/Path/id/屬性 屬性內容
注意:
a)如果操作的資料屬於集合類型(拿全部資料),那麼MIME類型字串應該以vnd.android.cursor.dir/開頭.
// Uri為content://com.ljq.provider.personprovider/person,那麼返回的MIME類型字串應該為:"vnd.android.cursor.dir/person"。
b)如果要操作的資料屬於非集合類型資料(拿單個資料),那麼MIME類型字串應該以vnd.android.cursor.item/開頭.
// Uri為content://com.ljq.provider.personprovider/person,那麼返回的MIME類型字串應該為:"vnd.android.cursor.item/person"。
c) 用 DESC 表示按倒序排序(即:從大到小排序)
// SORT_ORDER="_id DESC"
d) 用 ACS 表示按正序排序(即:從小到大排序)
5、UriMatcher匹配uri
UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 常量UriMatcher.NO_MATCH表示不匹配任何路徑的返回碼
sMatcher.addURI(“com.xxx.provider.myprovider”, “person”, 1);
//添加需要匹配uri,如果匹配就會返回匹配碼 對全部資料操作
// 如果match()方法匹配content://com.xxx.provider.myprovider/person路徑,返回匹配碼為1
sMatcher.addURI(“com.xxx.provider.myprovider”, “person / #”, 2);
// #號為萬用字元 對單個資料操作
//如果match()方法匹配content://com.xxx.provider.myprovider/person/230路徑,返回匹配碼為2
6、contentProvider在程式的操作中只是提供一個操作標準,所以需要用到Android.content.ContentResolve類完成,這個類的操作方法是和contentProvider一一對應的,調用它的方法就相當於調用了contentProvider的方法。
7、contentResolver是一個抽象類別,所以想要取得contentResolver類的執行個體化對象進行操作,則需要依靠Android.app.activity類的方法
Public contentResolve getcontentResolve()
8、uri的輔助操作類:contentUris
由於所有資料都要通過uri進行傳遞,以增加操作為例,當使用者執行完增加資料操作後,往往需要增加後的資料id通過uri進行返回,當接收到這個uri的時候就需要從裡面取得增加資料的id。
這時就用到了Android.content.contentUris的協助工具輔助類:
指定的uri中取出id:long parseId(uri contentUri)
指定的uri之後增加id參數: uri withAppendedId(uri ContentUri,long id)
8、SQLite的表格實現BaseColumns介面:
BaseColumns簡單的添加兩個欄位
public static final String _ID = "_id";
public static final String _COUNT = "_count";
使用通用名稱無論對其整體結構 (即其他非 ID 列) 以統一的方式可以解決任何資料的項目,Android 平台 (和開發人員以及)。在介面/類中定義的常用的字串常量可以避免一般和各地代碼的拼字錯誤。