[Android Pro] 监听内容提供者ContentProvider的数据变化

时间:2014-05-28 22:32:19   收藏:0   阅读:304

 转载自:http://blog.csdn.net/woshixuye/article/details/8281385

一、提出需求

有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者ContentProvider;A应用修改了B应用的数据,插入了一条数据。有这样一个需求,此时C应用需要得到数据被修改的通知并处理相应操作。

二、示例代码

 

bubuko.com,布布扣
A应用
/**
 * 对内容提供者进行操作
 * 
 * @author XY
 * 
 */
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;

/**
 * @author XY
 * 
 */
public class PersonProvider extends ContentProvider
{
    private DBOpeningHelper dbHelper;

    // 若不匹配采用UriMatcher.NO_MATCH(-1)返回
    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
    {
        // 对等待匹配的URI进行匹配操作,必须符合cn.xyCompany.providers.personProvider/person格式
        // 匹配返回CODE_NOPARAM,不匹配返回-1
        MATCHER.addURI("cn.xyCompany.providers.personProvider", "person", CODE_NOPARAM);

        // #表示数字 cn.xyCompany.providers.personProvider/person/10
        // 匹配返回CODE_PARAM,不匹配返回-1
        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:
                // 若主键值是自增长的id值则返回值为主键值,否则为行号,但行号并不是RecNo列
                long id = db.insert("person", "name", values); 
                Uri insertUri = ContentUris.withAppendedId(uri, id); 
                // 发出变化通知(非必须)设监听者为null。
                // 若设置某个监听者则不管有多少个监听者,该监听者一定可以获得该通知
                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)
        {
            // select * from person order by id desc limit 1 // 取得最近插入的值(序号大——>小并取第一个)
            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);
            }
        }
    }
}
bubuko.com,布布扣

 

 

 

[Android Pro] 监听内容提供者ContentProvider的数据变化,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!