这里通过监听内容提供者数据变化和监听发送短信进行讲解监听器。如果用户提供者数据发生了变化,监听器立刻就能收到对数据库操作的记录,而监听器是采用通知的机制,如果不采用通知的机制也可以做,那就是不断的查询数据库,这样的话效率就会很低。而采用通知机制的话,当用户向数据库发送一条记录,ContentObserver立刻就能获取到,我们就可以进行处理数据。
监听内容提供者数据的变化
1.在内容提供者中可以通知其他程序数据发生变化时进行监听
通过Context的getContentResolver()方法获取ContentResolver
调用其notifyChange()方法发送数据修改通知
2.在其他程序中可以通过ContentObserver监听数据变化
通过Context的getContentResolver()方法获取ContentResolver
调用其registerContentObserver()方法指定对某个Uri注册ContentObserver
自定义ContentObserver,重写onChange()方法获取数据
比如在用户插入数据的时候进行监听通知:
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = helper.getWritableDatabase();
- switch (matcher.match(uri)) {
- case PERSON_ALL:
- long id = db.insert("person", "id", values);
- // 监听通知
- getContext().getContentResolver().notifyChange(uri, null);
- return ContentUris.withAppendedId(uri, id);
- default:
- throw new IllegalArgumentException("Unmatch Uri: " + uri);
- }
- }
在另外一个工程中进行监听,一旦用户进行插入操作,我立刻就可以自动获取到用户插入的记录:
- public class MainActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getContentResolver().registerContentObserver(Uri.parse("content://cn.itcast.provider.itcast/person"), true, new MyObserver(new Handler()));
- }
-
- class MyObserver extends ContentObserver {
- public MyObserver(Handler handler) {
- super(handler);
- }
- //当监听到数据发生了变化就调用这个方法,并将新添加的数据查询出来
- public void onChange(boolean selfChange) {
- ContentResolver resolver = getContentResolver();
- Uri uri = Uri.parse("content://cn.itcast.provider.itcast/person");
- Cursor cursor = resolver.query(uri, new String[] { "id", "name", "phone", "balance" }, null, null, "id DESC LIMIT 1");
- while (cursor.moveToNext()) {
- System.out.print(cursor.getString(0) + " ");
- System.out.print(cursor.getString(1) + " ");
- System.out.print(cursor.getString(2) + " ");
- System.out.println(cursor.getString(3));
- }
- }
- }
- }
监听发送短信
1.Android系统提供了Provider对短信进行查询,当发出短信时也会发送更改通知
2.定义一个Observer监听"content://sms"
3.在onChange()方法中查询用户发送的短信"content://sms/outbox"
4.短信发送信息存放在数据库 date/date/com.android.providers.telephony
5.需要权限<uses-permission android:name="android.permission.READ_SMS" />
示例:
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler()));
- }
- //监听发送的短信
- private final class SmsObserver extends ContentObserver {
- public SmsObserver(Handler handler) {
- super(handler);
- }
-
- public void onChange(boolean selfChange) {
- ContentResolver resolver = getContentResolver();
- //查到发出的短信
- Uri uri = Uri.parse("content://sms/outbox");
- Cursor cursor = resolver.query(uri, new String[] { "date", "address", "body" }, null, null, "_id desc limit 1");
- if (cursor.moveToNext()) {
- long ms = cursor.getLong(0);
- String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(ms));
- String address = cursor.getString(1);
- String body = cursor.getString(2);
- System.out.println(date + " " + address + " " + body);
- }
- }
- }