ContentObserver类的使用
ContentObserver类的使用
application A提供了provider, application B可以使用.
application on更新provider后, 要通知application B, 而application B通过ContentObserver获取更新状态.
要在application A中设置访问权限, 并更新
在application A中的AndroidManifest.xml
中, 设置provider
权限
<application> <provider android:name="AProvider"> <path-permissionandroid:readPermission="android.permission.READ_EXTERNAL_STORAGE"android:writePermission="android.permission.WRITE_EXTERNAL_STORAGTE" /></application>
在application A中, provider中添加
public class AProvider extends ContentProvider { public update(uri, ...) { // do some work getContext().getContentProvider().notifyChange(uri, null); }}
application B通过ContentOberver获取更新消息
包括获取权限和设置ContentObserver.
获取权限
首先, application B使用application A的provider, 要在application B中的AndroidManifest.xml
中, 获取provider
权限. 写权限包含了读权限, 所以可以不用写读权限.
<uses-premission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-premission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
设置ContentObserver
有两种使用ContentObserver的方法.
方法1: 在cursor中调用cursor.registerContentObserver
, 不需要handler传递消息
在onCreate()
中使用cursor.registerContentObserver
, new一个Observer, 重写onChange()
这种方法比较简单 代码如下:
import android.database.Cursor;import android.database.ContentObserver;import android.provider.MediaStore;public class BActivity extends Activity { private Cursor mCursor; public onCreate() { mCursor = null; ContentProvider resolver = getContentResolver(); try {mCursor = resolver.query(uri, new String[] {MediaStore.Files.FileColumns._ID}, null, null, null);mCursor.registerContentObserver(new ContentObserver(null) { @Override public void onChange(boolean selfChange) {// do some work when obsering some changes } }); } catch (Exception e) {//TODO: handle exceptione.printStackTrace(); } } public onDestroy() { if (mCursor != null && mCursor.isClosed()) {mCursor.close();mCursor = null; } }}
方法2: 重写ContentObserver, 并使用handler来传递消息
1 创建特定的ContentObserver派生类, 重载父类构造方法, 重载
onChange()
方法处理回调后的功能实现2 利用
context.getContentResolver()
获得ContentResovler对象, 调用registerContentObserver()
方法注册3 ContentObserver的生命周期不同步于Activity和Service等, 在不需要时, 需要手动调用
unregisterContentObserver()
取消注册
重载ContentProvider
public class TwoContentObserver extends ContentObserver { private static String TAG = "TwoContentObserver"; private static int MSG_ONE_PROVIDER_CHANGE = 1; private Context mContext; private Handler mHandler; // use handler to update UI thread public TwoContentObserver(Context context, Handler handler) { super(handler); mContext = context; mHandler = handler; } @Overrride public void onChange(boolean selfChange) { Log.i(TAG, "--------TwoContentObserver change--------"); Uri outUri = Uri.parse(""); Cursor cursor = mContext.getContentResolver().query(outUri, null, null, null, null); if (c != null ) {StringBuilder stringBuilder = new StringBuilder();while (cursor.moveToNext()) { // do some work} } cursor.close(); mHandler.obtainMessage(MSG_ONE_PROVIDER_CHANGE, sb.toString().sendToTarget()); }}
在Activity调用
public class MainActivity extends Activity { private static final int MSG_ONE_PROVIDER_CHANGE = 1; private TwoContentObserver twoContentObserver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); twoContentObserver = new TwoContentObserver(this, mHandler); // 注册ContentObserver registerContentObservers(); } private void registerContentObservers() { Uri uri = Uri.parse(""); getContentResovler().registerContentObserver(uri, false, twoContentObserver); } private Handler mHanlder = new Handler() { publc void handlerMessage(Message msg) {switch(msg.what) { case MSG_ONE_PROVIDER_CHANGE: // do some work break; default: break;} } }; @Override public void onDestroy() { super.onDestroy(); // 取消注册 getContentResolver().unregisterContentObserver(twoContentObserver); }}
参考
http://www.cnblogs.com/slider/archive/2012/02/14/2351702.html