0

我想重新发送收到的 SMS_RECEIVED 广播。我在这里找到了示例:http ://blog.dev001.net/post/14085892020/android-generate-incoming-sms-from-within-your-app并以此类推:

boolean received=false;

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            if(!received) {

                received=true;

                Bundle pudsBundle = intent.getExtras();
                Object[] pdus = (Object[]) pudsBundle.get("pdus");
                SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);

                abortBroadcast();

                if(/*my condition here*/) {
                    Intent update = new Intent();
                    update.setClassName( "com.android.mms", "com.android.mms.transaction.SmsReceiverService");
                    update.setAction("android.provider.Telephony.SMS_RECEIVED");
                    update.putExtra( "pdus" , new Object[] { pdus });
                    startService(update);
                }

            }
        }
    }
};

但是这段代码会导致 com.android.mms 崩溃(我在 LogCat 中看到“I/ActivityManager(71): Process com.android.mms (pid 904) has dead.”):

threadid=8: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: SmsReceiverService
java.lang.ClassCastException: [Ljava.lang.Object;
    at android.provider.Telephony$Sms$Intents.getMessagesFromIntent(Telephony.java:617)
    at com.android.mms.transaction.SmsReceiverService.handleSmsReceived(SmsReceiverService.java:299)
    at com.android.mms.transaction.SmsReceiverService.access$100(SmsReceiverService.java:67)
    at com.android.mms.transaction.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:172)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.os.HandlerThread.run(HandlerThread.java:60)

如何解决?

4

1 回答 1

1

如果您想使用相同的数据(相同的收到的短信),最好重用相同的意图并且不要托盘构建您自己的。只需使用 intent.putExtras(Bundle extras); 你能替换“update.putExtra(“pdus”, new Object[] { pdus });”吗?使用“update.putExtra(pudsBundle)”。试试看,让我知道它是否有效,我没有时间复制。

于 2012-09-18T16:00:46.827 回答