3

I have a feeling i already know the answer to this but im not sure. Im using a broadcast receiver to intercept incoming SMS messages.

I register the receiver the typical way using the registerReceiver function and when you unregister the receiver you use the function unregisterReceiver function. I keep a global broadcast variable to load and unload the receiver as needed.

If the app closes, like the user actually closes the application and i don't unregister the receiver in the onDestroy method which i know is bad practice, would there be a way to unregister the receiver the next time the app loads? Could i create another instance of that broadcast receiver and then unregister it?

1 Answer 1

3

If the app closes

You can say that your UI is in the foreground or not. And you can say that your app's process is in the foreground, the background, or terminated.

It is unclear what "closing" would equate to.

like the user actually closes the application

A user can move an app's UI and process to the background (e.g., press HOME). A user can destroy an app's UI and move its process to the background (e.g., press BACK from the last running activity). A user can terminate an app's background process (e.g., swipe the app off of the recent-tasks list). A user can force-stop an app (e.g., pressing the Force Stop button for the app in Settings).

It is unclear what "the user actually closes the application" equates to.

would there be a way to unregister the receiver the next time the app loads?

Either you have a reference to your BroadcastReceiver object, or you do not. If you do, call unregisterReceiver() on some Context, passing in that BroadcastReceiver instance. If you do not have the BroadcastReceiver instance, you cannot unregister the receiver. If your process had been terminated between when you registered the receiver and now, that receiver is gone and is effectively unregistered.

2
  • 1
    when i say close the application i do not mean click the home button and send it to the background. I mean actually terminate the process. Does the receiver stay registered if the process is terminated?
    – CBaker
    Sep 26, 2014 at 16:01
  • @CBaker: "Does the receiver stay registered if the process is terminated?" -- no, because the receiver no longer exists. Sep 26, 2014 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.