Android keep BroadcastReceiver in background

23,343

Solution 1

This is not how you should register a receiver. You receiver stops working, because you construct it in onCreate, which means it will live as long as your app is alive. When the app gets destroyed, you also lose the the receiver.

If you register receiver inside an activity, you should always register it in onResume and deregister onPause, which will make it available while the activity is visible to the user. This is a use case when you want to have an active receiver while user interacts with an activity.

If you want a background receiver, you need to register it inside the AndroidManifest (with intent filter), add an IntentService and start it when you receive a broadcast in the receiver.

Here is a tutorial, you are interested in chapter 3.

If you need to be always on, start a foreground service. There is function in Service that lets you: startForeground. Then register your receiver when service is created and deregister when it's destroyed. Foreground services are quite nasty though.

Solution 2

Use a service with it. Services can survive when the app dies if they have the right flag example:

public class MyService extends Service {
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY; //this defines this service to stay alive
    }
@Override
public void onCreate() {
    super.onCreate();

    appStatus = APPISUP;

    //This is a thread that stays alive for as long as you need
    new CheckActivityStatus().execute();
    //Not needed but in case you wish to lauch other apps from it
}

private class CheckActivityStatus extends AsyncTask<String, Integer, String> {
 @Override
    protected String doInBackground(String... params) {
        while(true) {
        ... //add something that breaks eventually
        }
    }
 }

To lauch the service you have to lauch it from an activity like so:

Intent service = new Intent(getBaseContext(), MyService.class);
    startService(service);

With the service the BroadcastReceiver still functions receiving whatever you want. Note that the service sometimes stops and comes back. I haven't found out why but I'm betting on priorities of other apps that may ask the system to halt the service

Share:
23,343
User
Author by

User

Android Developer.

Updated on July 10, 2022

Comments

  • User
    User almost 2 years

    I created a BroadcastReceiver and it runs only when my app shown in recent apps menu. If I remove my app from the recent apps the BroadcastReceiver will stop working. How can I keep the BroadcastReceiver in background?

    I register the BroadcastReceiver from my main activity (in OnCreate()).

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(receiver, intentFilter);
    
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
    
        }   
    };
    
  • gruszczy
    gruszczy about 10 years
    OK, you must elaborate on what you are trying to achieve here then.
  • User
    User about 10 years
    I want to call a method when the battery is 50% (even if the app is closed).
  • Dan S
    Dan S about 10 years
    Register them in a Service that starts at boot.
  • User
    User about 10 years
    And how to keep the service in its own process?
  • gruszczy
    gruszczy about 10 years
    Added a comment about foreground service that you can use.
  • basickarl
    basickarl almost 4 years
    About the background receiver, will that work after reboot even if the app hasn't been started by the user (after the reboot that is)?