简体   繁体   中英

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM