如何在Android中捕获外发短信

时间:2014-08-21 12:41:33

标签: android

当手机在我的Android应用程序上发送短信时,我想抓住短信。我如何捕捉传出的短信?

我尝试使用contentResolver,但有时它不起作用。

谢谢

2 个答案:

答案 0 :(得分:0)

  

我如何捕捉传出的短信?

你没有。

  

我尝试使用contentResolver,但有时它不起作用。

不需要在任何ContentProvider中提供已发送的短信。直接使用SmsManager的应用可能不会向操作系统和邮件收件人以外的任何人显示其SMS消息。

答案 1 :(得分:0)

外发短信

您可以通过将内容观察者放在内容上来侦听传出短信:// sms / out但您无法使用本机短信应用修改它。您显然可以修改内容的内容:// sms / out但它有没有意义。

基本上,你必须注册一个内容观察者......就像这样:

ContentResolver contentResolver = context.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, yourObserver);

yourObserver是一个对象(new YourObserver(new Handler())),可能如下所示:

class YourObserver extends ContentObserver {

    public YourObserver(Handler handler) {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        // save the message to the SD card here
    }
}

那么,你究竟如何获得短信的内容?您必须使用光标:

// save the message to the SD card here
Uri uriSMSURI = Uri.parse("content://sms/out");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null, null, null);
 // this will make it point to the first record, which is the last SMS sent
cur.moveToNext();
String content = cur.getString(cur.getColumnIndex("body"));
// use cur.getColumnNames() to get a list of all available columns...
// each field that compounds a SMS is represented by a column (phone number, status, etc.)
// then just save all data you want to the SDcard :)
相关问题