窃听发出的短信

用户使用系统自带的短信程序发送短信,程序会通过ContentProvider把短信保存进数据库,并且发出一个数据变化通知,使用ContentObserver对数据变化进行监听,在用户发送短信时,就会被ContentObserver窃听到短信。

具体实现需要注意以下几个知识点:

Ÿ 定义一个Observer监听"content://sms"

Ÿ onChange()方法中查询"content://sms/outbox"

Ÿ 短信发送数据库在 com.android.providers.telephony

Ÿ 需要权限android.permission.READ_SMS


具体实现:

注册监听:

getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, 
new SmsObserver(new Handler()));


监听类SmsObserver:

private final class SmsObserver extends ContentObserver{
public SmsObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {//查询发送箱中的短信(处于正在发送状态的短信放在发送箱)
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"),null, null, null, null); 
while(cursor.moveToNext()){
StringBuilder sb = new StringBuilder();
sb.append("_id=").append(cursor.getInt(cursor.getColumnIndex("_id")));
sb.append(",address=").append(cursor.getString(cursor.getColumnIndex("address")));
sb.append(";body=").append(cursor.getString(cursor.getColumnIndex("body")));
sb.append(";time=").append(cursor.getLong(cursor.getColumnIndex("date")));
Log.i("ReceiveSendSMS", sb.toString());
             } }    
}


你可能感兴趣的:(android,短信,窃听)