问题
I want to resend received SMS_RECEIVED broadcast. I've find example here: http://blog.dev001.net/post/14085892020/android-generate-incoming-sms-from-within-your-app and make it by analogy:
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);
}
}
}
}
};
But this code crash com.android.mms (I see "I/ActivityManager(71): Process com.android.mms (pid 904) has died." in LogCat) with this:
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)
How to fix it?
回答1:
If you want to use the same data( same received sms) it would be better to reuse the same intent and don't tray to build your own. just use intent.putExtras(Bundle extras); Can you replace "update.putExtra( "pdus" , new Object[] { pdus });" with "update.putExtra(pudsBundle)". Try it and let me know if it works, i haven't time to reproduce.
来源:https://stackoverflow.com/questions/12479930/is-it-possible-to-resend-sms-received-broadcast