内容观察观察者主要是观察数据库的改变
package xidian.dy.com.chujia; 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.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver cr = getContentResolver(); cr.registerContentObserver(Uri.parse("content://sms"), true, new MyObserver(new Handler()) ); } static class MyObserver extends ContentObserver { public MyObserver(Handler handler) { super(handler); } //当数据库发生改变时该方法会被调用 @Override public void onChange(boolean selfChange) { super.onChange(selfChange); } } }
内容观察者能正常运行需要内容提供这发出数据库改变的通知,短信内容提供者已经实现了该功能,所以可以观测到数据库的改变。