Android中ContentProvider組件資料共用

來源:互聯網
上載者:User

Android中ContentProvider組件資料共用

ContentProvider的功能和意義:

主要用於對外共用資料,也就是通過ContentProvider把應用中的資料共用給其他應用訪問,其他應用可以通過ContentProvider對指定應用中的資料進行操作。

實現在不同應用程式之間共用資料,在應用程式之間交換資料,一個應用程式A把自己的資料通過ContentProvider暴露給其他程式使用B,B就可以通過ContentResolver來操作ContentProvider暴露的資料,包括增,刪,查,改

1、ContentProvider使用表的形式來組織資料
   無論資料的來源是什麼,ContentProvider都會認為是一種表,然後把資料群組織成表格
2、ContentProvider提供的方法
public boolean onCreate() 在建立ContentProvider時調用
public Cursor query(Uri, String[], String, String[], String) 用於查詢指定Uri的ContentProvider,返回一個Cursor
public Uri insert(Uri, ContentValues) 用於添加資料到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用於更新指定Uri的ContentProvider中的資料
public int delete(Uri, String, String[]) 用於從指定Uri的ContentProvider中刪除資料
public String getType(Uri) 用於返回指定的Uri中的資料的MIME類型
*如果操作的資料屬於集合類型,那麼MIME類型字串應該以vnd.android.cursor.dir/開頭。
例如:要得到所有person記錄的Uri為content://contacts/person,那麼返回的MIME類型字串為"vnd.android.cursor.dir/person"。
*如果要操作的資料屬於非集合類型資料,那麼MIME類型字串應該以vnd.android.cursor.item/開頭。
例如:要得到id為10的person記錄的Uri為content://contacts/person/10,那麼返回的MIME類型字串應為"vnd.android.cursor.item/person"。
3、每個ContentProvider都有一個公用的URI,這個URI用於表示這個ContentProvider所提供的資料。Android所提供的ContentProvider都存放在android.provider包當中

 

二,Uri類簡介

1,為系統的每一個資源給其一個名字,比方說通話記錄。每個ContentProvider都有一個公用的URI,這個URI用於表示這個ContentProvider所提供的資料。

例Uri:content://com.hust.uri/words

Uri指定了將要操作的ContentProvider,其實可以把一個Uri看作是一個網址,我們把Uri分為三部分。
第一部分是"content://"。可以看作是網址中的"http://"。
第二部分是主機名稱或authority,用於唯一標識這個ContentProvider,外部應用需要根據這個標識來找到它。可以看作是網址中的主機名稱,比如"blog.csdn.net"。
第三部分是路徑名,資源部分,用來表示將要操作的資料。

 

例:content://com.hust.uri/words

訪問的資源是words/2,意味著訪問word資料表中的全部資料

例:content://com.hust.uri/words/2

訪問的資源是words/2,意味著訪問word資料中ID為2的記錄

 

例:content://com.hust.uri/words/2/word

訪問的資源是words/2,意味著訪問word資料中ID為2的記錄的word欄位

把一個字串轉換成Uri,是Uri類的靜態方法:

Uri uri = Uri.parse("content://com.hust.uri/contact")

 

2,UriMatcher類使用介紹

因為Uri代表了要操作的資料,所以我們經常需要解析Uri,並從Uri中擷取資料。Android系統提供了兩個用於操作Uri的工具類,分別為UriMatcher和ContentUris

UriMatcher類用於匹配Uri:

A,void addURI(String authority,String path,int code)向UriMatcher中註冊Uri,authority和path組成一個Uri,code代表該Uri對應的標識碼

B,int match(Uri uri);根據前面註冊的Uri來判斷指定的Uri對應的標識碼

 

UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);matcher.addURI("com.hust.uri","words",1); matcher.addURI("com.hust.uri","words/#",2); 
com.hust.uri/words/#表示words下的所有資料uri的表示為2

 

匹配結果如下:

 

matcher.match(Uri.parse("content://com.hust.uri/words"));//返回標識碼1matcher.match(Uri.parse("content://com.hust.uri/words/2"));//返回標識碼2matcher.match(Uri.parse("content://com.hust.uri/words/10"));//返回標識碼2
註冊完需要匹配的Uri後,就可以使用sMatcher.match(uri)方法對輸入的Uri進行匹配,如果匹配就返回匹配碼,匹配碼是調用addURI()方法傳入的第三個參數

 

3,ContentUris類使用介紹

ontentUris類用於操作Uri路徑後面的ID部分,它有兩個比較實用的方法:
withAppendedId(uri, id)用於為路徑加上ID部分:

Uri resultUri = ContentUris.withAppendedId(uri, 10); //產生後的Uri為:content://com.hust.personprovider/person/10
parseId(uri)方法用於從路徑中擷取ID部分:
Uri uri = Uri.parse("content://com.hust.uri.personprovider/person/10")long personid = ContentUris.parseId(uri);//擷取的結果為:10

三,開發ContentProvider

 

1,開發一個ContentProvider子類,繼承ContentProvider,實現query,insert,update,delete等方法

2,在Manifest.xml資訊清單檔中註冊該ContentProvider,指定其Android:authorities屬性

 

public class FirstProvider extends ContentProvider{// 第一次建立該ContentProvider時調用該方法@Overridepublic boolean onCreate(){System.out.println("===onCreate方法被調用===");return true;}// 該方法的傳回值代表了該ContentProvider所提供資料的MIME類型@Overridepublic String getType(Uri uri){System.out.println("~~getType方法被調用~~");return null;}// 實現查詢方法,該方法應該返回查詢得到的Cursor@Overridepublic Cursor query(Uri uri, String[] projection, String where,String[] whereArgs, String sortOrder){System.out.println(uri + "===query方法被調用===");System.out.println("where參數為:" + where);return null;}// 實現插入的方法,該方法應該新插入的記錄的Uri@Overridepublic Uri insert(Uri uri, ContentValues values){System.out.println(uri + "===insert方法被調用===");System.out.println("values參數為:" + values);return null;}// 實現刪除方法,該方法應該返回被刪除的記錄條數@Overridepublic int delete(Uri uri, String where, String[] whereArgs){System.out.println(uri + "===delete方法被調用===");System.out.println("where參數為:" + where);return 0;}// 實現刪除方法,該方法應該返回被更新的記錄條數@Overridepublic int update(Uri uri, ContentValues values, String where,String[] whereArgs){System.out.println(uri + "===update方法被調用===");System.out.println("where參數為:"+ where + ",values參數為:" + values);return 0;}}

這4個方法用於供其他應用通過ContentProvider調用。
註冊ContentProvider:

android:authorities屬性是一定要配置的,指定該ContentProvider對應的Uri

 

上面配置指定了該ContentProvider被綁定到“content://org.providers.firstprovider”,這意味著其他應用的ContentResovler向該Uri執行query,insert,update,delete方法時,實際上是調用該ContentProvider的query,insert,update,delete方法,ContentResovler調用方法時將參數傳給ContentProvider對應的方法參數。

 

四,開發contentResovler

通過getContentResolver()方法擷取ContentResolver對象,擷取ContentResolver對象之後,接下來可調用query,insert,update,delete方法了,實際上調用的是指定Uri對應的ContentProvider的query,insert,update,delete方法

例:介面4個按鈕,分別對應增刪查改功能:

 

public class FirstResolver extends Activity{ContentResolver contentResolver;//聲明變數Uri uri = Uri.parse("content://org.providers.firstprovider/");//FirstProvider的Uri@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 擷取系統的ContentResolver對象contentResolver = getContentResolver();}public void query(View source){// 調用ContentResolver的query()方法。// 實際返回的是該Uri對應的ContentProvider的query()的傳回值Cursor c = contentResolver.query(uri, null, "query_where", null, null);Toast.makeText(this, "遠程ContentProvide返回的Cursor為:" + c,Toast.LENGTH_LONG).show();}public void insert(View source){ContentValues values = new ContentValues();values.put("name", "fkjava");// 調用ContentResolver的insert()方法。// 實際返回的是該Uri對應的ContentProvider的insert()的傳回值Uri newUri = contentResolver.insert(uri, values);Toast.makeText(this, "遠程ContentProvide新插入記錄的Uri為:"+ newUri, Toast.LENGTH_LONG).show();}public void update(View source){ContentValues values = new ContentValues();values.put("name", "fkjava");// 調用ContentResolver的update()方法。// 實際返回的是該Uri對應的ContentProvider的update()的傳回值int count = contentResolver.update(uri, values, "update_where", null);Toast.makeText(this, "遠程ContentProvide更新記錄數為:"+ count, Toast.LENGTH_LONG).show();}public void delete(View source){// 調用ContentResolver的delete()方法。// 實際返回的是該Uri對應的ContentProvider的delete()的傳回值int count = contentResolver.delete(uri, "delete_where", null);Toast.makeText(this, "遠程ContentProvide刪除記錄數為:"+ count, Toast.LENGTH_LONG).show();}


 

五,ContentProvider與ContentResolver的關係

ContentResolver對指定的Uri指定CRUD等資料操作,但是Uri並不是真正的資料中心,因此這些CRUD操作會委託給Uri對應的ContentProvider來實現。通常來說,A應用通過ContentResolver執行CRUD操作,這些操作都需要指定Uri參數,Android系統就根據該Uri找到對應的ContentProvider(該ContentProvider通常屬於B應用),ContentProvider則負責實現CRUD方法,完成對底層資料的增刪改查等操作,這樣A應用就可以訪問,修改B應用的資料了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.