一、监听ContentProvider主要步骤
1.在ContentProvider类的insert\update\delete方法加入this.getContext().getContentResolver().notifyChange(URI,null);
其中URI表示监听的URI
null表示发送消息给任何人;
2.在访问者的类中调用如下方法:
context.getContentResolver().registerContentObserver(uri,true,new ContentObserver(new Handler()));
3.创建内部类继承ContentObserver并重写下面两个方法:
(1)带有Handler的构造函数;
(2)public void onChange(boolean selfChange); 此函数在内容提供者发出改变信号时就会被调用
参数selfChange为如果是自己改变的原因,则为true;如果不是自己改变的,则为false;
二、实例
![](https://img-my.csdn.net/uploads/201207/01/1341141899_9710.png)
我们在Test1和Test2应用都对ContentProvider的数据改变进行注册,如果ContentProvider发出通知,则Test1和Test2都会收到通知;
注意:此处有一个很重要的问题,如果Test1和Test2应用的MainActivity为同一个包,则如果先运行Test2,再运行Test1,则Test2会停止运行,因为Test1应用会覆盖Test2的应用,即接收不到通知;
因此我们要把Test1应用和Test2应用的包名设为不同,此处为org.xiazdong和org.xzdong;
ContentProvider.java
package org.xiazdong.db; <strong>
</strong>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);
@Override
public boolean onCreate() {
helper = new DatabaseHelper(this.getContext());
matcher.addURI("org.xiazdong.provides.personprovider", "person", 1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return null;
}
/*
* 如果操作集合,则必须以vnd.android.cursor.dir开头
* 如果操作非集合,则必须以vnd.android.cursor.item开头
* */
@Override
public String getType(Uri uri) {
return "";
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = helper.getWritableDatabase();
switch(matcher.match(uri)){
case 1:
long rowid = db.insert("person", null, values);
this.getContext().getContentResolver().notifyChange(uri, null);//如果改变数据,则通知所有人
return ContentUris.withAppendedId(uri, rowid); //返回插入的记录所代表的URI
default:
throw new IllegalArgumentException("wrong uri");
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}
Test1应用
package org.xiazdong;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
private OnClickListener listener = new OnClickListener(){
@Override
public void onClick(View v) {
ContentResolver resolver = MainActivity.this.getContentResolver();
Uri uri = Uri.parse("content://org.xiazdong.provider2/person");
resolver.registerContentObserver(uri, true, new PersonObserver(new Handler()));
ContentValues values = new ContentValues();
values.put("name", "zzz");
values.put("age", 1);
resolver.insert(uri, values); //向ContentProvider插入数据
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(listener);
}
private class PersonObserver extends ContentObserver{//监听
public PersonObserver(Handler handler) {
super(handler);
}
//当ContentProvier数据发生改变,则触发该函数
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("Test1", "数据改变");
}
}
}
Test2应用
package org.xzdong;
import android.app.Activity;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri uri = Uri.parse("content://org.xiazdong.provider2/person");
this.getContentResolver().registerContentObserver(uri, true, new PersonObserver(new Handler()));
}
private class PersonObserver extends ContentObserver{
public PersonObserver(Handler handler) {
super(handler);
}
//当ContentProvier数据发生改变,则触发该函数
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.v("Test2", "数据改变");
}
}
}
效果: