I have a service that sends sms messages (via AsyncTask). I need to capture the send/receive status with the help of BraodcaseReceiver. For some reason I can't get the correct resultCode from the Receiver. If I implement onReceive in my Service, the resultCode is always null.
Not sure what I'm doing wrong, so the questions what is the correct way to send message from a Broadcase Receiver class back to a background service?
Background:
The application consists of one activity that does nothing but running a background service. In that service I send an sms messages.
I am not very good at Android design patterns, but from what I've seen on the internet, to get result of sms status, I need to create a separate class as BraodcaseReceiver. That's why I created one. That BroadcastReceiver should get the status of the sent message, and what I want is to be able to pass that value to the service (later on from the service to activity)
So what I'm doing is: Aactivty->Service->SendSMS, then on task complete, I'd like to receive the status of the sms sent.
Note: I've just realized that I didn't use AsyncTask to send the sms. I want to use the service only as a manager of the AsyncTasks that send the sms messages, and I don't want them the sending the messages to block the service. Is this a good design?
Service:
protected boolean sendSMS(String number) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
String destinationAddress = number;
String smsMessage = String.format("This is test");
String scAddress = null;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(destinationAddress, scAddress, smsMessage, sentPI, deliveredPI);
return true;
}
BroadcastReceiver sendBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Integer resultCode = intent.getExtras().getInt("msg");
//resultCode is always null here
Log.d("Debug", "sendBroadcastReceiver code: "+ resultCode);
}
};
BroadcastReceveier:
public class SentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Integer resultcode = getResultCode();
//**for some reason when I define onReceive in Service class**
//**I don't get any debug message as if this code doesn't execute**
Log.d("Debug", "Code: "+ resultcode);
Intent intent = new Intent("SMS_SENT");
intent.putExtra("msg", resultcode);
context.sendBroadcast(intent);
}
}