2

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);
    }
}
Lamar
  • 1,761
  • 4
  • 24
  • 50
  • It's not clear what you're trying to do, exactly, but those Receivers are a little mixed up. With the current setup, the `sendBroadcastReceiver` in the `Service` is where you'd check the result code; i.e., where you'd call `getResultCode()`. I'm not sure what purpose the other `BroadcastReceiver` is meant to serve. – Mike M. Nov 29 '18 at 01:02
  • Sorry, I added some more information about what that code actually does. Hopefully it's clearer now. If you think my approach was not correct, please by all means correct my code. – Lamar Nov 29 '18 at 12:11
  • You don't necessarily need a separate Receiver class for this. You have one too many Receivers, atm. As is, your `SENT` `PendingIntent` will fire `sendBroadcastReceiver`. That other `SentReceiver` isn't doing anything. Move the request code stuff into `sendBroadcastReceiver`, and remove the `context.sendBroadcast()` code. – Mike M. Nov 29 '18 at 12:31
  • I'm afraid I can't follow you.. Can you please explain in a separate answer? This way I and others will benefit from your answer. Many thanks! – Lamar Nov 29 '18 at 12:37
  • Well, honestly, this is a duplicate, as this has been demonstrated in many posts here already, so I'd rather not answer. Simply delete everything you currently have inside `sendBroadcastReceiver`'s `onReceive()`, then replace it with `int resultcode = getResultCode();` and `Log.d("Debug", "Code: "+ resultcode);`. That's it. You don't need `SentReceiver` at all, for this setup. – Mike M. Nov 29 '18 at 12:44
  • Ok thanks anyway. One thing I've forgot to explicitly mention (yet visible in the code sample). I need another BroadcastReceiver for delivery status, not only send status. Should I dot it the same way? – Lamar Nov 29 '18 at 12:47
  • 1
    You can use the same Receiver for both. You just have to register it with an `IntentFilter` with both actions. I have an answer that shows that here: https://stackoverflow.com/a/24845193. – Mike M. Nov 29 '18 at 12:50
  • 1
    Thanks a lot! Will check it. – Lamar Nov 29 '18 at 12:53

0 Answers0