android Content Observer内容观察者基本使用
package com.example.observertest; import android.content.ContentResolver; import android.database.ContentObserver; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://sms/"); resolver.registerContentObserver(uri, true, new MyObserver(new Handler())); } private class MyObserver extends ContentObserver { public MyObserver(Handler handler) { super(handler); } //当内容观察者 观察到是数据库的内容变化了调用这个方法
//观察到消息邮箱里面有一条数据库内容变化的通知
@Override public void onChange(boolean selfChange) { super.onChange(selfChange); Toast.makeText(MainActivity.this, "数据库内容发生了改变", 1).show(); } } }