Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
public class ScreenTestActivity extends Activity {
/** Called when the
activity is first created. */
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SmsContent
content = new SmsContent(new Handler());
//
注册短信变化监听
this.getContentResolver().registerContentObserver(
Uri.parse("content://sms/"),
true, content);
}
class SmsContent extends ContentObserver {
private Cursor cursor =
null;
public SmsContent(Handler handler) {
super(handler);
}
/**
* @Description 当短信表发送改变时,调用该方法 需要两种权限
android.permission.READ_SMS读取短信
* android.permission.WRITE_SMS写短信
*
@Author Snake
* @Date 2010-1-12
*/
@Override
public void
onChange(boolean selfChange) {
// TODO Auto-generated method
stub
super.onChange(selfChange);
// 读取收件箱中指定号码的短信
cursor =
managedQuery(Uri.parse("content://sms/inbox"),
new String[] { "_id",
"address", "read" },
" address=? and read=?",
new String[] {
"15555215556", "0" }, "date desc");
if (cursor != null) {
ContentValues values = new
ContentValues();
values.put("read", "1"); //
修改短信为已读模式
cursor.moveToFirst();
while (cursor.isLast()) {
//
更新当前未读短信状态为已读
getContentResolver().update(
Uri.parse("content://sms/inbox"),
values, " _id=?",
new String[] { "" + cursor.getInt(0)
});
//getContentResolver().delete(Uri.parse("content://sms/inbox"), " _id=?",
new String[] { "" + cursor.getInt(0)
});
cursor.moveToNext();
}
}
}
}
}
Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户,,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。