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")); //返回標識碼1 matcher.match(Uri.parse("content://com.hust.uri/words/2")); //返回標識碼2 matcher.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 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:
<!-- 注冊一個ContentProvider --><providerandroid:exported="true"android:name=".FirstProvider"android:authorities="org.providers.firstprovider"></provider> 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應用的數據了
總結
以上是生活随笔為你收集整理的Android中ContentProvider组件数据共享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android数据存储的三种方式-Sha
- 下一篇: Android中的Service组件详解