Android ContentObserver ---拦截短信并删除

class SMSDBObserver extends ContentObserver {

private Context ctx;

public SMSDBObserver(Handler handler, Context ctx) {
super(handler);
this.ctx = ctx;
}

@SuppressWarnings("static-access")
public void onChange(boolean selfChange) {
super.onChange(selfChange);
try {
// 准备系统短信收信箱的uri地址
Uri uri = Uri.parse("content://sms/inbox");
// 查询收信箱里所有的短信
Cursor isRead = ctx.getContentResolver().query(uri, null, "read=" + 0, null, null);
while (isRead.moveToNext()) {
//String phone = isRead.getString(isRead.getColumnIndex("address")).trim();
String body = isRead.getString(isRead.getColumnIndex("body")).trim();
Log.i("bindphone", "get the sms---->" + body);
if (body!=null && (body.indexOf("Vimi")!=-1) && (body.indexOf(App.bindphonenum)!=-1)) {
Log.i("bindphone", "get the validate sms---->" + body);
int id = isRead.getInt(isRead.getColumnIndex("_id"));
PalmmessagerService.instance.validateBindPhone(App.bindphonearea, App.bindphonenum);
ctx.getContentResolver().delete(Uri.parse("content://sms"), "_id=" + id, null);
holdsms = false;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}





//开始监听验证短信
Handler handler = null;
if (smsobserver == null) {// 创建内容监听器 短信监听
try{
handler = new Handler();
}catch(Exception e){
Log.i("bindphone", "get handler error :" + e.getMessage());
}
smsobserver = new SMSDBObserver(handler, this);
}
if (!holdsms) {// 注册内容监听器,并指定内容监听器,监听那一个Uri的数据变化
Log.i("bindphone", "start monitor the sms");
this.getContentResolver().registerContentObserver(
Uri.parse("content://sms/"), true, smsobserver);
holdsms = true;
}

你可能感兴趣的:(exception,android,String,null,Class,sms)