Android開發之ContentProvider(內容提供者)

來源:互聯網
上載者:User

標籤:des   android   class   c   ext   a   

1、 ContentProvider簡介

當應用繼承ContentProvider類,並重寫該類用於提供資料和儲存資料的方法,就可以向其他應用共用其資料。雖然使用其他方法也可以對外共用資料,但資料訪問方式會因資料存放區的方式而不同。

如:採用檔案方式對外共用資料,需要進行檔案操作讀寫資料;採用sharedpreferences共用資料,需要使用sharedpreferences API讀寫資料。

而使用ContentProvider共用資料的好處是統一了資料訪問方式

2、通過ContentProvider對外共用資料的步驟:

第一步 建立一個類繼承ContentProvider並根據重寫下面方法(下面的方法並不是都要實現)

public class PersonContentProvider extends ContentProvider{

   public boolean onCreate()

   public Uri insert(Uri uri, ContentValues values)

   public int delete(Uri uri, String selection, String[] selectionArgs)

   public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

   public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

   public String getType(Uri uri)

}

第二步需要在AndroidManifest.xml使用<provider>對該ContentProvider進行配置,為了能讓其他應用找到該ContentProvider , ContentProvider 採用了authorities(主機名稱/網域名稱)對它進行唯一標識。

<manifest .... >

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <provider android:name=".PersonContentProvider" android:authorities="cn.itcast.provider.personprovider"/>

    </application>

</manifest>

注意:一旦應用繼承了ContentProvider類,後面我們就會把這個應用稱為ContentProvider(內容提供者)。

 

 3、URI

每Uri代表了要操作的資料,Uri主要包含了兩部分資訊:

1>需要操作的ContentProvider

2>對ContentProvider中的什麼資料進行操作

一個Uri由以下幾部分組成:

 content://com.<公司名>.provider.應用程式名稱/person/10

主機名稱(或叫Authority)用於唯一標識這個ContentProvider,外部調用者可以根據這個標識來找到它。android:authority採取類似網站網域名稱來給它賦值。一般這樣來定義authority:

com.<公司名>.provider.應用程式名稱

路徑(path)可以用來表示我們要操作的資料,路徑的構建應根據業務而定,如下:

要操作person表中id為10的記錄,可以構建這樣的路徑:/person/10

要操作person表中id為10的記錄的name欄位, person/10/name

要操作person表中的所有記錄,可以構建這樣的路徑:/person

要操作xxx表中的記錄,可以構建這樣的路徑:/xxx

當然要操作的資料不一定來自資料庫,也可以是檔案等他儲存方式,如下:

要操作xml檔案中person節點下的name節點,可以構建這樣的路徑:/person/name

如果要把一個字串轉換成Uri,可以使用Uri類中的parse()方法,如下:

Uri uri =Uri.parse("content:// com.<公司名>.provider.應用程式名稱/資料路徑 ")

把一個字串轉化為Uri

4、 UriMatcher類使用介紹(public classUriMatcher)

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

A、public class UriMatcher

B、publicclass ContentUris

首先第一步把你需要匹配Uri路徑全部給註冊上,如下:

//常量UriMatcher.NO_MATCH表示不匹配任何路徑的返回碼

UriMatcher  sMatcher = newUriMatcher(UriMatcher.NO_MATCH);

//如果match()方法匹配content://cn.itcast.provider.personprovider/person路徑,返回匹配碼為1

sMatcher.addURI(“cn.itcast.provider.personprovider”,“person”, 1);

//添加需要匹配uri,如果匹配就會返回匹配碼(匹配碼可以自己任意取)

//如果match()方法匹配content://cn.itcast.provider.personprovider/person/230路徑,返回匹配碼為2

sMatcher.addURI(“cn.itcast.provider.personprovider”,“person/#”, 2);//#號為萬用字元

switch (sMatcher.match(Uri.parse("content://cn.itcast.provider.personprovider/person/10"))){

   case1

   break;

  case 2

   break;

  default://不匹配

    break;

}

註冊完需要匹配的Uri後,就可以使用sMatcher.match(uri)方法對輸入的Uri進行匹配,如果匹配就返回匹配碼,匹配碼是調用addURI()方法傳入的第三個參數,假設匹配content://cn.itcast.provider.personprovider/person路徑,返回的匹配碼為1

代碼如下:

private static final UriMatcher uriMatcher;

static{

     uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

   uriMatcher.addURI(“com.<公司名>.provider.應用程式名稱”,”items”,1);

   uriMatcher.addURI(“com.<公司名>.provider.應用程式名稱”,”items/#”,2);

}

 

5、ContentUris類使用介紹

ContentUris類用於擷取Uri路徑後面的ID部分,它有兩個比較實用的方法:

withAppendedId(uri,id)用於為路徑加上ID部分:

Uri uri =Uri.parse("content://cn.itcast.provider.personprovider/person")

Uri resultUri =ContentUris.withAppendedId(uri, 10);

//產生後的Uri為:content://cn.itcast.provider.personprovider/person/10

 

parseId(uri)方法用於從路徑中擷取ID部分:

Uri uri =Uri.parse("content://cn.itcast.provider.personprovider/person/10")

long personid = ContentUris.parseId(uri);//擷取的結果為:10

6、public abstract class ContentProvider

ContentProvider類主要方法的作用:

public boolean onCreate()

該方法在ContentProvider建立後就會被調用, Android在系統啟動時就會建立ContentProvider。

public Uri insert(Uri uri, ContentValuesvalues)

該方法用於供外部應用往ContentProvider添加資料。

public int delete(Uri uri, Stringselection, String[] selectionArgs)

該方法用於供外部應用從ContentProvider刪除資料。

public int update(Uri uri, ContentValuesvalues, String selection, String[] selectionArgs)

該方法用於供外部應用程式更新ContentProvider中的資料。

public Cursor query(Uri uri, String[]projection, String selection, String[] selectionArgs, String sortOrder)

該方法用於供外部應用從ContentProvider中擷取資料。

public String getType(Uri uri)

該方法用於返回當前Url所代表資料的MIME類型。如果操作的資料屬於集合類型,那麼MIME類型字串應該以vnd.android.cursor.dir/開頭。

例如:要得到所有person記錄的Uri為content://cn.itcast.provider.personprovider/person,那麼返回的MIME類型字串應該為:“vnd.android.cursor.dir/person”。如果要操作的資料屬於單一資料,那麼MIME類型字串應該以vnd.android.cursor.item/開頭,例如:得到id為10的person記錄,Uri為content://cn.itcast.provider.personprovider/person/10,那麼返回的MIME類型字串應該為:“vnd.android.cursor.item/person”。

7、使用ContentResolver操作ContentProvider中的資料

當外部應用需要對ContentProvider中的資料進行添加、刪除、修改和查詢操作時,可以使用ContentResolver 類來完成,要擷取ContentResolver對象,可以使用Activity提供的getContentResolver()方法。 ContentResolver 類提供了與ContentProvider類相同簽名的四個方法:

public Uri insert(Uri uri, ContentValuesvalues)

該方法用於往ContentProvider添加資料。

public int delete(Uri uri, Stringselection, String[] selectionArgs)

該方法用於從ContentProvider刪除資料。

public int update(Uri uri, ContentValuesvalues, String selection, String[] selectionArgs)

該方法用於更新ContentProvider中的資料。

public Cursor query(Uri uri, String[]projection, String selection, String[] selectionArgs, String sortOrder)

該方法用於從ContentProvider中擷取資料。

 

這些方法的第一個參數為Uri,代表要操作的是哪個ContentProvider和對其中的什麼資料進行操作,假設給定的是:Uri.parse(“content://cn.itcast.provider.personprovider/person/10”),那麼將會對主機名稱為cn.itcast.provider.personprovider的ContentProvider進行操作,操作的資料為person表中id為10的記錄。

使用方法:

1>得到ContentResolver對象。

2>定義一個Uri,ContentValues對象。

3>調用ContentResolver對象的方法。

使用ContentResolver對ContentProvider中的資料進行添加、刪除、修改和查詢操作:

ContentResolver resolver =  getContentResolver();

Uri uri = Uri.parse("content://cn.itcast.provider.personprovider/person");

//添加一條記錄

ContentValues values = new ContentValues();

values.put("name", "itcast");

values.put("age", 25);

resolver.insert(uri, values);          

//擷取person表中所有記錄

Cursor cursor = resolver.query(uri, null, null, null, "personid desc");

while(cursor.moveToNext()){

       Log.i("ContentTest", "personid="+ cursor.getInt(0)+ ",name="+ cursor.getString(1));

}

//把id為1的記錄的name欄位值更改新為liming

ContentValues updateValues = new ContentValues();

updateValues.put("name", "liming");

Uri updateIdUri = ContentUris.withAppendedId(uri, 2);

resolver.update(updateIdUri, updateValues, null, null);

//刪除id為2的記錄

Uri deleteIdUri = ContentUris.withAppendedId(uri, 2);

resolver.delete(deleteIdUri, null, null);

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.