分类分类
更新时间:2026-03-11 18:34:32作者:zhao
ContentObserver的使用类似与设计模式中的观察者模式,ContentObserver是观察者,被观察的ContentProvider是被观察者。当被观察者ContentProvider的数据发生了增删改的变化,就会及时的通知给ContentProvider,ContentObsserver做出相应的处理。
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());
}
}
}
}
相关
遮天帝路争锋手游角色扮演266.95 MBv1.12026-03-11
下载深海迷航冰点之下手机版角色扮演2.08Gv1.22.549392026-03-11
下载旺商聊App社交通讯100.11 MBv3.6.32026-03-11
下载火箭沙盒App实用工具37.4 MBv1.2.62026-03-11
下载免费刷短剧漫剧影音播放111.08 MBv1.0.12026-03-11
下载免费漫剧大全APP影音播放93.03 MBv1.0.02026-03-11
下载全免漫剧看短剧影音播放103.79 MBv1.0.12026-03-11
下载武将无双0.1折版策略游戏605.69 MBv0.12026-03-11
下载Canta实用工具4.48 Mv3.2.22026-03-11
下载王者圣域策略游戏429.26 Mv2.1.0.22026-03-11
下载光强仪app实用工具4.91 Mv5.1.02026-03-11
下载光之守护者最新版角色扮演323.66 Mv1.8.92026-03-11
下载










