基础总结篇之八:创建及调用自己的ContentProvider
转自:http://blog.csdn.net/wellsoho/article/details/49494141
若不能坚持到底,即使是朽木也不能折断;只要坚持不停地用刀刻,就算是金属玉石也可以雕出花饰。用今天的话来说就是:再容易的事情,没有锲而不舍的精神,都不可能做到;再难的事情,只要有坚持不懈的努力,都一定能够做到。希望我们在坚持理想的道路上都能够锲而不舍地雕刻自己的那块“金石”。
今天我们来讲解一下如何创建及调用自己的ContentProvider。
在前面两篇文章中我们分别讲了如何读写联系人和短消息,相信大家对于ContentProvider的操作方法已经有了一定程度的了解。在有些场合,除了操作ContentProvider之外,我们还有可能需要创建自己的ContentProvider,来提供信息共享的服务,这就要求我们很好的掌握ContentProvider的创建及使用技巧。下面我们就由表及里的逐步讲解每个步骤。
在正式开始实例演示之前,我们先来了解以下两个知识点:
授权:
在Android中,每一个ContentProvider都会用类似于域名的字符串来注册自己,我们成为授权(authority)。这个唯一标识的字符串是此ContentProvider可提供的一组URI的基础,有了这个基础,才能够向外界提供信息的共享服务。
授权是在AndroidManifest.xml中完成的,每一个ContentProvider必须在此声明并授权,方式如下:
- <provider android:name=".SomeProvider"
- android:authorities="com.your-company.SomeProvider"/>
MIME类型:
就像网站返回给定URL的MIME(Multipurpose Internet Mail Extensions,多用途Internet邮件扩展)类型一样(这使浏览器能够用正确的程序来查看内容),ContentProvider还负责返回给定URI的MIME类型。根据MIME类型规范,MIME类型包含两部分:类型和子类型。例如:text/html,text/css,text/xml等等。
Android也遵循类似的约定来定义MIME类型。
对于单条记录,MIME类型类似于:
vnd.android.cursor.item/vnd.your-company.content-type
而对于记录的集合,MIME类型类似于:
vnd.android.cursor.dir/vnd.your-company.comtent-type
其中的vnd表示这些类型和子类型具有非标准的、供应商特定的形式;content-type可以根据ContentProvider的功能来定,比如日记的ContentProvider可以为note,日程安排的ContentProvider可以为schedule,等等。
了解了以上两个知识点之后,我们就结合实例来演示一下具体的过程。
我们将会创建一个记录person信息的ContentProvider,实现对person的CRUD操作,访问者可以通过下面路径操作我们的ContentProvider:
访问者可以通过“[BASE_URI]/persons”来操作person集合,也可以通过“[BASE_URI]/persons/#”的形式操作单个person。
我们创建一个person的ContentProvider需要两个步骤:
1.创建PersonProvider类:
我们需要继承ContentProvider类,实现onCreate、query、insert、update、delete和getType这几个方法。具体代码如下:
- package com.scott.provider;
- 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 static final UriMatcher matcher;
- private DBHelper helper;
- private SQLiteDatabase db;
- private static final String AUTHORITY = "com.scott.provider.PersonProvider";
- private static final int PERSON_ALL = 0;
- private static final int PERSON_ONE = 1;
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.scott.person";
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.scott.person";
- //数据改变后立即重新查询
- private static final Uri NOTIFY_URI = Uri.parse("content://" + AUTHORITY + "/persons");
- static {
- matcher = new UriMatcher(UriMatcher.NO_MATCH);
- matcher.addURI(AUTHORITY, "persons", PERSON_ALL); //匹配记录集合
- matcher.addURI(AUTHORITY, "persons/#", PERSON_ONE); //匹配单条记录
- }
- @Override
- public boolean onCreate() {
- helper = new DBHelper(getContext());
- return true;
- }
- @Override
- public String getType(Uri uri) {
- int match = matcher.match(uri);
- switch (match) {
- case PERSON_ALL:
- return CONTENT_TYPE;
- case PERSON_ONE:
- return CONTENT_ITEM_TYPE;
- default:
- throw new IllegalArgumentException("Unknown URI: " + uri);
- }
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
- db = helper.getReadableDatabase();
- int match = matcher.match(uri);
- switch (match) {
- case PERSON_ALL:
- //doesn't need any code in my provider.
- break;
- case PERSON_ONE:
- long _id = ContentUris.parseId(uri);
- selection = "_id = ?";
- selectionArgs = new String[]{String.valueOf(_id)};
- break;
- default:
- throw new IllegalArgumentException("Unknown URI: " + uri);
- }
- return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- int match = matcher.match(uri);
- if (match != PERSON_ALL) {
- throw new IllegalArgumentException("Wrong URI: " + uri);
- }
- db = helper.getWritableDatabase();
- if (values == null) {
- values = new ContentValues();
- values.put("name", "no name");
- values.put("age", "1");
- values.put("info", "no info.");
- }
- long rowId = db.insert("person", null, values);
- if (rowId > 0) {
- notifyDataChanged();
- return ContentUris.withAppendedId(uri, rowId);
- }
- return null;
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- db = helper.getWritableDatabase();
- int match = matcher.match(uri);
- switch (match) {
- case PERSON_ALL:
- //doesn't need any code in my provider.
- break;
- case PERSON_ONE:
- long _id = ContentUris.parseId(uri);
- selection = "_id = ?";
- selectionArgs = new String[]{String.valueOf(_id)};
- }
- int count = db.delete("person", selection, selectionArgs);
- if (count > 0) {
- notifyDataChanged();
- }
- return count;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
- db = helper.getWritableDatabase();
- int match = matcher.match(uri);
- switch (match) {
- case PERSON_ALL:
- //doesn't need any code in my provider.
- break;
- case PERSON_ONE:
- long _id = ContentUris.parseId(uri);
- selection = "_id = ?";
- selectionArgs = new String[]{String.valueOf(_id)};
- break;
- default:
- throw new IllegalArgumentException("Unknown URI: " + uri);
- }
- int count = db.update("person", values, selection, selectionArgs);
- if (count > 0) {
- notifyDataChanged();
- }
- return count;
- }
- //通知指定URI数据已改变
- private void notifyDataChanged() {
- getContext().getContentResolver().notifyChange(NOTIFY_URI, null);
- }
- }
在我们的PersonProvider中,我们用到了Person、DBHelper类,代码如下:
- package com.scott.provider;
- public class Person {
- public int _id;
- public String name;
- public int age;
- public String info;
- public Person() {
- }
- public Person(String name, int age, String info) {
- this.name = name;
- this.age = age;
- this.info = info;
- }
- }
- package com.scott.provider;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DBHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "provider.db";
- private static final int DATABASE_VERSION = 1;
- public DBHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String sql = "CREATE TABLE IF NOT EXISTS person" +
- "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER, info TEXT)";
- db.execSQL(sql);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS person");
- onCreate(db);
- }
- }
最后,要想让这个ContentProvider生效,我们需要在AndroidManifest.xml中声明并为其授权,如下所示:
- <provider android:name=".PersonProvider"
- android:authorities="com.scott.provider.PersonProvider"
- android:multiprocess="true"/>
完成了ContentProvider后,下面我们来看一下访问者。这一步我们在MainActivity中完成,看下面代码:
- package com.scott.provider;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.CursorWrapper;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- public class MainActivity extends Activity {
- private ContentResolver resolver;
- private ListView listView;
- private static final String AUTHORITY = "com.scott.provider.PersonProvider";
- private static final Uri PERSON_ALL_URI = Uri.parse("content://" + AUTHORITY + "/persons");
- private Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- //update records.
- requery();
- };
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- resolver = getContentResolver();
- listView = (ListView) findViewById(R.id.listView);
- //为PERSON_ALL_URI注册变化通知
- getContentResolver().registerContentObserver(PERSON_ALL_URI, true, new PersonObserver(handler));
- }
- /**
- * 初始化
- * @param view
- */
- public void init(View view) {
- ArrayList<Person> persons = new ArrayList<Person>();
- Person person1 = new Person("Ella", 22, "lively girl");
- Person person2 = new Person("Jenny", 22, "beautiful girl");
- Person person3 = new Person("Jessica", 23, "sexy girl");
- Person person4 = new Person("Kelly", 23, "hot baby");
- Person person5 = new Person("Jane", 25, "pretty woman");
- persons.add(person1);
- persons.add(person2);
- persons.add(person3);
- persons.add(person4);
- persons.add(person5);
- for (Person person : persons) {
- ContentValues values = new ContentValues();
- values.put("name", person.name);
- values.put("age", person.age);
- values.put("info", person.info);
- resolver.insert(PERSON_ALL_URI, values);
- }
- }
- /**
- * 查询所有记录
- * @param view
- */
- public void query(View view) {
- // Uri personOneUri = ContentUris.withAppendedId(PERSON_ALL_URI, 1);查询_id为1的记录
- Cursor c = resolver.query(PERSON_ALL_URI, null, null, null, null);
- CursorWrapper cursorWrapper = new CursorWrapper(c) {
- @Override
- public String getString(int columnIndex) {
- //将简介前加上年龄
- if (getColumnName(columnIndex).equals("info")) {
- int age = getInt(getColumnIndex("age"));
- return age + " years old, " + super.getString(columnIndex);
- }
- return super.getString(columnIndex);
- }
- };
- //Cursor须含有"_id"字段
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2,
- cursorWrapper, new String[]{"name", "info"}, new int[]{android.R.id.text1, android.R.id.text2});
- listView.setAdapter(adapter);
- startManagingCursor(cursorWrapper); //管理Cursor
- }
- /**
- * 插入一条记录
- * @param view
- */
- public void insert(View view) {
- Person person = new Person("Alina", 26, "attractive lady");
- ContentValues values = new ContentValues();
- values.put("name", person.name);
- values.put("age", person.age);
- values.put("info", person.info);
- resolver.insert(PERSON_ALL_URI, values);
- }
- /**
- * 更新一条记录
- * @param view
- */
- public void update(View view) {
- Person person = new Person();
- person.name = "Jane";
- person.age = 30;
- //将指定name的记录age字段更新为30
- ContentValues values = new ContentValues();
- values.put("age", person.age);
- resolver.update(PERSON_ALL_URI, values, "name = ?", new String[]{person.name});
- //将_id为1的age更新为30
- // Uri updateUri = ContentUris.withAppendedId(PERSON_ALL_URI, 1);
- // resolver.update(updateUri, values, null, null);
- }
- /**
- * 删除一条记录
- * @param view
- */
- public void delete(View view) {
- //删除_id为1的记录
- Uri delUri = ContentUris.withAppendedId(PERSON_ALL_URI, 1);
- resolver.delete(delUri, null, null);
- //删除所有记录
- // resolver.delete(PERSON_ALL_URI, null, null);
- }
- /**
- * 重新查询
- */
- private void requery() {
- //实际操作中可以查询集合信息后Adapter.notifyDataSetChanged();
- query(null);
- }
- }
在前面的PersonProvider我们也提到,在数据更改后,会向指定的URI访问者发出通知,以便于更新查询记录。大家注意,仅仅是ContentProvider出力还不够,我们还需要在访问者中注册一个ContentObserver,才能够接收到这个通知。下面我们创建一个PersonObserver:
- package com.scott.provider;
- import android.database.ContentObserver;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- public class PersonObserver extends ContentObserver {
- public static final String TAG = "PersonObserver";
- private Handler handler;
- public PersonObserver(Handler handler) {
- super(handler);
- this.handler = handler;
- }
- @Override
- public void onChange(boolean selfChange) {
- super.onChange(selfChange);
- Log.i(TAG, "data changed, try to requery.");
- //向handler发送消息,更新查询记录
- Message msg = new Message();
- handler.sendMessage(msg);
- }
- }
最后,我们要在AndroidManifest.xml中为MainActivity添加MIME类型过滤器,告诉系统MainActivity可以处理的信息类型:
- <!-- MIME类型 -->
- <intent-filter>
- <data android:mimeType="vnd.android.cursor.dir/vnd.scott.person"/>
- </intent-filter>
- <intent-filter>
- <data android:mimeType="vnd.android.cursor.item/vnd.scott.person"/>
- </intent-filter>
鉴于操作类型太多,我在这里就不再展示了,大家可以自己试一试。
来源:http://blog.csdn.net/liuhe688/article/details/7050868
- 顶
- 0
- 踩