天天看點

Android開發14——監聽内容提供者ContentProvider的資料變化

一、提出需求

有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;  

 // 若不比對采用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);  

 public boolean onCreate()  

  dbHelper = new DBOpeningHelper(this.getContext());  

  return true;  

 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.os.Bundle;  

import android.os.Handler;  

  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);  

   }  

關于contentProvider的基本使用,請參看本部落格博文《Android開發13——内容提供者ContentProvider的基本使用》

本文轉自IT徐胖子的專欄部落格51CTO部落格,原文連結http://blog.51cto.com/woshixy/1085439如需轉載請自行聯系原作者

woshixuye111

繼續閱讀