Android检测失败消息的内容

时间:2013-12-19 16:05:36

标签: android text sms broadcastreceiver android-pendingintent

所以我目前正在开发一个涉及使用SMSManager标准实例发送短信的应用程序。虽然连续发送许多消息时似乎存在相对较高的故障率。是否可以检测失败文本消息的内容(包括消息正文和收件人)。这是我当前检测消息是否失败的方式。顺便说一下,sis对象是另一个类对未发送消息作出反应的回调。

    PendingIntent intent;
 intent = PendingIntent.getBroadcast(context, 0,
            new Intent("SMS_SENT"), 0);


registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
//send callback that a text message was successful
                    sis.messageSent();
                    break;
                default:
                    Toast.makeText(getApplicationContext(), "Message unsent", Toast.LENGTH_SHORT);
                    Bundle unsent = arg1.getExtras();
//send a call back that the message was unsent
//im trying to get the contents of the unsent message and pass it to another activity via a bundle but the pending intent is not returning any values(I checked it with a debugger)                    
sis.messageFailed();
                    break;
            }
        }
    }, new IntentFilter("SMS_SENT"));

1 个答案:

答案 0 :(得分:0)

消息是在intent extras中传递的,你可以这样得到它:

Bundle bundle=intent.getExtras();

Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];

for(int n=0;n<messages.length;n++){
    sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}

如果短信很短,您可以通过以下方式获取消息文本:

String messageText = sms[0].getMessageBody();  

如果很长,你应该迭代短信数组