So this is my BroadcastReceiver
public class IncomingSMSListener extends BroadcastReceiver {
private static final String SMS_EXTRA_NAME = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] messages = fetchSMSMessagesFromIntent(intent);
}
private SmsMessage[] fetchSMSMessagesFromIntent(Intent intent) {
ArrayList<SmsMessage> receivedMessages = new ArrayList<SmsMessage>();
Object[] messages = (Object[]) intent.getExtras().get(SMS_EXTRA_NAME);
for (Object message : messages) {
SmsMessage finalMessage = SmsMessage
.createFromPdu((byte[]) message);
receivedMessages.add(finalMessage);
}
return receivedMessages.toArray(new SmsMessage[0]);
}
}
I’m being able to read the incoming message just fine and all, but let’s say from here I want to forward the message to another phone number and make sure it got sent. I know I can do SmsManager.sendTextMessage() but how do I set up the PendingIntent part to be notified whether the SMS got sent or not?
OK, ended up finding the solution in the end. Since the context passed in to the onReceive() method in the BroadCastReceiver doesn’t let me register other BroadcastReceivers to listen for the “message sent” event, I ended up getting a grip of the app context and doing what follows:
In the BroadcastReceiver:
SENT_SMS_FLAG is simply a static string that uniquely identifies the intent I just made. My MessageSentListener looks like this:
}