【问题标题】:Android detect contents of failed messageAndroid检测失败消息的内容
【发布时间】:2013-12-19 16:05:35
【问题描述】:

所以我目前正在开发一个应用程序,该应用程序涉及使用 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"));

【问题讨论】:

    标签: android text sms broadcastreceiver android-pendingintent


    【解决方案1】:

    消息是通过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]);
    }
    

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

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

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

    【讨论】:

    • 非常感谢。过去几周我一直在努力解决这个问题!
    猜你喜欢
    • 2019-12-28
    • 2010-09-27
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    相关资源
    最近更新 更多