Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i send information from broadcast receiver back to my main activity without restarting it

  if (messages.length > -1) {
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, "Message recieved: " + messages[0].getOriginatingAddress(), duration);
                        toast.show();
                        String value = "Message recieved: " + messages[0].getOriginatingAddress();

my code here gets an sms. i want to bring it back to my main activity but i CANT use i intent and restart that activity(the broadcast reciever is a background service and people will be using the app during this.) instead of toasting how can i pass that string back to the activity (i only have one main one).

like image 956
Dnaso Avatar asked Dec 06 '25 08:12

Dnaso


1 Answers

If you want to have your activity notified only when is in foreground, just throw a new broadcast from the sms broadcast receiver and intercept it from a broadcast receiver hosted in your activity.

On the other hand, if you want to check if the broadcast was sent when the activity comes back to foreground, even if the sms was received while the activity was in background, you can still use a broadcast receiver but you need to add a sticky intent which can be received afterwards.

Plus: use a local broadcast receiver in order to save resources.

like image 173
fedepaol Avatar answered Dec 08 '25 22:12

fedepaol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!