一、提出需求
有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者ContentProvider;A应用修改了B应用的数据,插入了一条数据。有这样一个需求,此时C应用需要得到数据被修改的通知并处理相应操作。
二、示例代码
- A应用
-
- public class MainActivity extends Activity
- {
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- public void insert(View v)
- {
- Uri uri = Uri.parse("content://cn.xyCompany.providers.personProvider/person");
- ContentResolver resolver = this.getContentResolver();
- ContentValues values = new ContentValues();
- values.put("name", "xy_new_new");
- values.put("phone", "xy_new_111");
- resolver.insert(uri, values);
- }
- }
-
-
- B应用
- package cn.xy.cotentProvider.app.providers;
- 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;
- import android.util.Log;
- import cn.xy.cotentProvider.service.DBOpeningHelper;
-
-
- public class PersonProvider extends ContentProvider
- {
- private DBOpeningHelper dbHelper;
-
-
- private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
-
-
- private static final int CODE_NOPARAM = 1;
- private static final int CODE_PARAM = 2;
-
- static
- {
-
-
- MATCHER.addURI("cn.xyCompany.providers.personProvider", "person", CODE_NOPARAM);
-
-
-
- MATCHER.addURI("cn.xyCompany.providers.personProvider", "person/#", CODE_PARAM);
- }
-
- @Override
- public boolean onCreate()
- {
- dbHelper = new DBOpeningHelper(this.getContext());
- return true;
- }
-
-
- @Override
- public Uri insert(Uri uri, ContentValues values)
- {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- switch (MATCHER.match(uri))
- {
- case CODE_NOPARAM:
-
- long id = db.insert("person", "name", values);
- Uri insertUri = ContentUris.withAppendedId(uri, id);
-
-
- getContext().getContentResolver().notifyChange(uri, null);
- return insertUri;
- default:
- throw new IllegalArgumentException("this is unkown uri:" + uri);
- }
- }
- ......
- }
-
-
- C应用
- package cn.xt.contentProvider.lisenter;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.database.ContentObserver;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
-
- public class MainActivity extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Uri uri = Uri.parse("content://cn.xyCompany.providers.personProvider/person");
- ContentResolver resolver = this.getContentResolver();
- resolver.registerContentObserver(uri, true, new PersonContentObserver(new Handler()));
- }
-
- private class PersonContentObserver extends ContentObserver
- {
- public PersonContentObserver(Handler handler)
- {
- super(handler);
- }
-
-
- @Override
- public void onChange(boolean selfChange)
- {
-
- Uri uri = Uri.parse("content://cn.xyCompany.providers.personProvider/person");
- ContentResolver resolver = MainActivity.this.getContentResolver();
- Cursor cursor = resolver.query(uri, null, null, null, "id desc limit 1");
- if(cursor.moveToFirst())
- {
- String name = cursor.getString(cursor.getColumnIndex("name"));
- Log.i("lisenter", name);
- }
- }
- }
- }
-
关于contentProvider的基本使用,请参看本博客博文《Android开发13——内容提供者ContentProvider的基本使用》
本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/1085439如需转载请自行联系原作者
woshixuye111