Android之ContentProvider詳細內容

ContentProvider(內容提供者)可以向其他應用共享其數據

ContentProvider 作爲應用程序數據的外部接口。ContentProvider 可以通過和數據庫接口類似的 query()、 insert()、update()和 delete()方法將特定的數據集暴露給其他應用程序

與其他對外共享數據的不同:統一了數據訪問方式

如何通過ContentProvider對外共享數據:
  1. 繼承ContentProvider並重寫下面方法:
public class MyContentProvider extends ContentProvider {
    //初始化數據,該方法在ContentProvider創建後就會被調用,Android在系統啓動時就會創建ContentProvider
    public boolean onCreate()
    //用於外部應用往ContentProvider獲取數據
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) 
    //用於返回當前Uri所代表數據的MIME類型
    public String getType(@NonNull Uri uri) 
     //用於外部應用往ContentProvider添加數據
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) 
     //用於外部應用往ContentProvider刪除數據
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) 
     //用於外部應用往ContentProvider更新數據
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) 
}
  1. 在AndroidManifest.xml對該ContentProvider進行配置,爲了讓其他應用能夠找到該ContentProvider,ContentProvider採用了authorities對它進行唯一標識,外部調用者可以通過這個唯一標識來找到它
<application...>
    <provider
        android:authorities="com.example.provider.myprovider"
        android:name=".MyContentProvider"/>
</application>
Uri介紹

Uri表示要操作的數據,Uri主要包含了兩部分信息:

  1. 需要操作的ContentProvider
  2. 對ContentProvider的什麼數據進行操作

Uri的組成:
在這裏插入圖片描述
如果要把字符串轉換成Uri,可以使用Uri類的parse()方法

Uri uri=Uri.parse("content://com.example.provider.myprovider/person");
Uri的類型

集合類型:MIME類型字符串爲"vnd.android.cursor.dir/開頭"
單一數據:MIME類型字符串爲"vnd.android.cursor.item/開頭"

UriMatcher類的介紹

Uri代表要操作的數據,所以我們經常需要解析Uri,並從Uri獲取數據
Android提供了兩個操作Uri的工具類,分別爲UriMatcherContentUris
UriMatcher用於匹配Uri,用法如下:
註冊需要匹配的Uri路徑

//常量UriMatcher.NO_MATCH表示不匹配任何路徑的返回碼
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//註冊需要的Uri
//第三個參數匹配碼
//兩個通配符:用來匹配數字的井號(#)和用來匹配任意文本的星號(*)
uriMatcher.addURI("com.example.provider.myprovider","/person",1);
uriMatcher.addURI("com.example.provider.myprovider","/person/#",2);
//使用smtch()方法對輸入的Uri進行匹配,如果匹配就返回匹配碼
switch (uriMatcher.match(Uri.parse("content://com.example.provider.myprovider/person"))){
    case 1:
        break;
    case 2:
        break;
    default://不匹配
        break;
}

ContentUris用於獲取Uri後面的ID部分,它有兩個比較實用的方法
withAppendedld(uri,id)用於往路徑加上ID部分

Uri uri =Uri.parse("content://com.example.provider.myprovider/person")
Uri resultUri =ContentUris.withAppendedId(uri, 10);
//生成後的Uri爲:content://com.example.provider.myprovider/person/10

parseId(uri)用於從路徑中獲取ID部分

Uri uri =  Uri.parse("content://com.example.provider.myprovider/person");
long i =  ContentUris.parseId(uri);
ContentResolver

獲取ContentResolver對象,可以使用Activity提供的getContentResolver()方法
ContentResolver提供了和ContentProvider簽名相同的四個方法,用於外部應用對ContentProvider中的數據進行增、刪、改、查
ContentResolver resolver = getContentResolver();
Uri uri =Uri.parse(“content://com.examples.personprovider/friends”);
//添加一條記錄

ContentValues values = new ContentValues();
values.put(PersonProvider.Columns.FIRST,"XiaoHong");
        values.put(PersonProvider.Columns.LAST, "Li");
values.put(PersonProvider.Columns.PHONE, "155");
resolver.insert(uri, values);
//獲取person表中所有記錄
Cursor cursor = resolver.query(uri, null,null, null, null);
while(cursor.moveToNext()){
    Log.i("ContentTest","id="+ cursor.getString(0)+ ",firstname="+cursor.getString(1)+",lastname="+cursor.getString(2)+",phone="+cursor.getString(3));
}

//把當前最後一個的記錄的name字段值更改新爲liming
ContentValues updateValues = new ContentValues();
updateValues.put(PersonProvider.Columns.FIRST,"liming");
cursor.moveToLast();
Uri updateIdUri = ContentUris.withAppendedId(uri, cursor.getInt(0));
resolver.update(updateIdUri, updateValues,null, null);
Cursor cursori = resolver.query(uri, null,null, null, null);
Log.i("ContentTest", "更新中。。。。。");
while(cursori.moveToNext()){
    Log.i("ContentTest","id="+ cursori.getString(0)+ ",firstname="+cursori.getString(1)+",lastname="+cursori.getString(2)+",phone="+cursori.getString(3));
}
cursor.close();
cursori.close();
//刪除所有
resolver.delete(uri, null, null);

整理自http://shouce.jb51.net/android-14-days/9.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章