Android监听器(2)
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" />
示例:
view plain
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");
相关新闻>>
- 发表评论
-
- 最新评论 更多>>