這裡通過監聽內容提供者數據變化和監聽發送短信進行講解監聽器。如果用戶提供者數據發生了變化,監聽器立刻就能收到對數據庫操作的記錄,而監聽器是采用通知的機制,如果不采用通知的機制也可以做,那就是不斷的查詢數據庫,這樣的話效率就會很低。而采用通知機制的話,當用戶向數據庫發送一條記錄,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);
- }
- }
- }