Android入門:ContentProvider

來源:互聯網
上載者:User

 

一、ContentProvider介紹

 

ContentProvider翻譯為“內容提供者”;

定義:指該應用程式套件含一些方法,供外界訪問,其他應用程式可以調用該方法,比如如果應用A建立了一個資料庫“test.db”,預設是私人的,即其他應用程式不能對其進行操作,但是如果應用A使用了ContentProvider,則其他應用程式可以訪問該資料庫;

用途:某個應用對外共用資料;

注意點:和Activity一樣,都是Android 的Component之一,如果要建立,則需要在AndroidManifest.xml中設定;

好處:提供了統一的insert,update,delete,query方法,操作任何資料;

 

二、URI介紹

 

URI:類似於我們以前使用的URI,但是此處URI的目的是為了能夠根據URI以及調用的方法來決定怎樣操作資料,比如:

uri="..../person",調用insert方法,則表示需要插入一條person記錄;

ContentProvider中使用的URI注意點:

(1)以  content://   開頭;

(2)模式為:content://authorities/path;其中authorities類似於網域名稱或IP,用來標識操作哪個ContentProvider,path表示具體的操作;

舉例:

content://org.xiazdong.providers.personprovider/person 表示調用“org.xiazdong.providers.personprovider”的方法,操作person資料;

 

補充:ContentUris輔助類

 

URI uri = ContentUris.withAppendId(URI param,int id);  //為某個URI添加一個ID

比如param = "content://authorities/person",id=10,則uri = "content://authorities/person/10";

long id = ContentUris.parseId(URI uri); //提取URI中最後的ID

比如uri = "content://authorities/person/10",則返回的id=10;

 

三、ContentProvider開發步驟簡要說明

 

1.建立一個類,並繼承ContentProvider,比如PersonProvider;

2.在AndroidManifest.xml中設定:

<provider android:name=".PersonProvider" android:authorities="org.xiazdong.provides.personprovider"/>

3.定義UriMatcher,

private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  //建立一個URI匹配器,參數為不匹配時的傳回值

在onCreate中使用matcher.addURI("authorities","path",code);  //加入匹配的URI,如果匹配,則matcher.match(uri)返回code;

如果要匹配:content://authorities/path/數字  ,則matcher.addURI("authorites","path/#",code);

4.重寫:

onCreate():用於為操作資料做準備;

insert:插入資料,返回插入的記錄所代表的URI;

update:更新資料,返回操作影響的記錄行數;

delete:刪除資料,返回操作影響的記錄行數;

query:查詢資料,返回Cursor;

getType:記錄的類型,如果操作集合,則必須以vnd.android.cursor.dir開頭,如果操作非集合,則必須以vnd.android.cursor.item開頭,比如vnd.android.cursor.dir/person

5.外部調用:

ContentResolver resolver = this.getContext().getContentResolver();

resolver.insert();

resolver.update();

resolver.delete();

resolver.query();

 

四、應用執行個體

 

AndroidManifest.xml

<provider            android:name=".PersonProvider"android:authorities="org.xiazdong.provides.personprovider"                        ></provider>

 

PersonProvider.java

package org.xiazdong.db;import android.content.ContentProvider;import android.content.ContentUris;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.net.Uri;public class PersonProvider extends ContentProvider{private DatabaseHelper helper;private SQLiteDatabase db;private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);@Overridepublic boolean onCreate() {helper = new DatabaseHelper(this.getContext());//匹配:content://org.xiazdong.provides.personprovider/person,傳回值為1matcher.addURI("org.xiazdong.provides.personprovider", "person", 1);//匹配:content://org.xiazdong.provides.personprovider/person/數字,傳回值為2matcher.addURI("org.xiazdong.provides.personprovider", "person/#", 2);return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {db = helper.getWritableDatabase();switch(matcher.match(uri)){case 1://查詢所有記錄return db.query("person", projection, selection, selectionArgs, null, null, null);case 2://查詢特定記錄long id = ContentUris.parseId(uri);String where = "id="+id;if(selection!=null&&!"".equals(selection)){//因為selection可能還包含其他的where語句,需要再加上 "and id=?"where = where + " and "+selection;}return db.query("person", projection, where, selectionArgs, null, null, null);default:throw new IllegalArgumentException("wrong uri");}}/* * 如果操作集合,則必須以vnd.android.cursor.dir開頭 * 如果操作非集合,則必須以vnd.android.cursor.item開頭 * */@Overridepublic String getType(Uri uri) {switch(matcher.match(uri)){case 1:return "vnd.android.cursor.dir/person";case 2:return "vnd.android.cursor.item/person";}}/* * values為插入的資料 * 返回:插入的資料所代表的URI * */@Overridepublic Uri insert(Uri uri, ContentValues values) {db = helper.getWritableDatabase();switch(matcher.match(uri)){case 1:long rowid = db.insert("person", null, values);return ContentUris.withAppendedId(uri, rowid);//返回插入的記錄所代表的URIdefault:throw new IllegalArgumentException("wrong uri");}}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {db = helper.getWritableDatabase();switch(matcher.match(uri)){case 1:return db.delete("person", selection, selectionArgs);case 2://刪除特定id記錄long id = ContentUris.parseId(uri);String where = "id="+id;if(selection!=null&&!"".equals(selection)){where  += " and "+selection;}return db.delete("person", where, selectionArgs);default:throw new IllegalArgumentException("wrong uri");}}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {db = helper.getWritableDatabase();switch(matcher.match(uri)){case 1:return db.update("person", values, selection, selectionArgs);case 2://更新特定id記錄long id = ContentUris.parseId(uri);String where = "id="+id;if(selection!=null&&!"".equals(selection)){where  += " and "+selection;}return db.update("person", values,where, selectionArgs);default:throw new IllegalArgumentException("wrong uri");}}}

以下我們建立一個測試類別:

 

ContentProviderTest.java

 

package org.xiazdong.db.test;import org.xiazdong.db.domain.Person;import android.content.ContentResolver;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.test.AndroidTestCase;import android.util.Log;public class ContentProviderTest extends AndroidTestCase{public void testInsert()throws Exception{//插入"name=yyy,age=100"的記錄Uri uri = Uri.parse("content://org.xiazdong.provides.personprovider/person");ContentResolver resolver = this.getContext().getContentResolver();ContentValues values = new ContentValues();values.put("name", "yyy");values.put("age", 100);resolver.insert(uri, values);}public void testUpdate()throws Exception{//更新id=5的記錄為name=yyy,age=100Uri uri = Uri.parse("content://org.xiazdong.provides.personprovider/person/5");ContentResolver resolver = this.getContext().getContentResolver();ContentValues values = new ContentValues();values.put("name", "yyy");values.put("age", 100);resolver.update(uri, values, null, null);}public void testDelete()throws Exception{//刪除id=11的記錄Uri uri = Uri.parse("content://org.xiazdong.provides.personprovider/person/5");//刪除id=11的記錄ContentResolver resolver = this.getContext().getContentResolver();resolver.delete(uri, null, null);}public void testQuery()throws Exception{//插入全部記錄並顯示Uri uri = Uri.parse("content://org.xiazdong.provides.personprovider/person");//查詢所有記錄ContentResolver resolver = this.getContext().getContentResolver();Cursor cursor = resolver.query(uri, null, null, null, null);while(cursor.moveToNext()){Person person = new Person(cursor.getInt(cursor.getColumnIndex("id")),cursor.getString(cursor.getColumnIndex("name")),cursor.getInt(cursor.getColumnIndex("age")));Log.v("ContentProvider", person.toString());}}}

 

聯繫我們

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