飞行模式状态、蓝牙状态、屏幕亮度值等,并且提供了相应的Uri。
2、观察系统的短信息数据发生了变化。当监听到短信数据发生变化时,查询所有已发送的短信并且显示出来。
短信的Uri共有一下几种:
- content://sms/inbox 收件箱
- content://sms/sent 已发送
- content://sms/draft 草稿
- content://sms/outbox 发件箱 (正在发送的信息)
- content://sms/failed 发送失败
-
content://sms/queued 待发送列表 (比如开启飞行模式后,该短信就在待发送列表里)
使用举例:
package com.example.android_contentobserver; import android.app.Activity; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册观察者Observser this.getContentResolver().registerContentObserver(Uri.parse("content://sms"),true,new SMSObserver(new Handler())); } private final class SMSObserver extends ContentObserver { public SMSObserver(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { Cursor cursor = MainActivity.this.getContentResolver().query( Uri.parse("content://sms/inbox"), null, null, null, null); while (cursor.moveToNext()) { StringBuilder sb = new StringBuilder(); sb.append("address=").append( cursor.getString(cursor.getColumnIndex("address"))); sb.append(";subject=").append( cursor.getString(cursor.getColumnIndex("subject"))); sb.append(";body=").append( cursor.getString(cursor.getColumnIndex("body"))); sb.append(";time=").append( cursor.getLong(cursor.getColumnIndex("date"))); System.out.println("--------has Receivered SMS::" + sb.toString()); } } } }










