How to make Broadcast Receiver run when my app is in background?

Google has deprecated registering Broadcast Receiver into manifest like this below from API Level 26+ ( Except Some )

<receiver android:name=".MyBroadcastReceiver"  android:exported="true">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

But, If one wants to receive particular device state changes like Internet Connectivity Changes (Which isn’t allowed) while the app is in background and if it’s important for any feature of his application, what should he do?

 

Solution What I Can Suggest :

When I was going through the documentation, My eyes got stuck here :

Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.

That practically means if I can hold a Context, the broadcast-receiver registered with it will run in the background.

For doing that, a Service will be the best practice.

This is below code for a STICKY_SERVICE which is started again after killed and thus the context remains valid.

AlwaysOnService.class

package app.exploitr.auto;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class AlwaysOnService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerReceiver(new ClickReceiver(), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
        return Service.START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onLowMemory() {  // rem this if you want it always----
        stopSelf();
        super.onLowMemory();
    }
}

Now, the receiver which actually does things :

ClickReceiver.class

package app.exploitr.auto;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.Objects;

public class ClickReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        switch (Objects.requireNonNull(intent.getAction())) {

            case AutoJob.NOTIFICATION_CANCEL_TAG:
                System.out.println("Not related");
                break;

            case AutoJob.LOGIN_CANCEL_TAG:
                System.out.println("Not related");
                break;

            case "android.net.conn.CONNECTIVITY_CHANGE":
                System.out.println("Oops! It works...");
                break;
        }
    }
}

Launch Code From Any Activity Class

private void setUpBackOffWork() {
    if (DataMan.getInstance(getBaseContext()).getPeriodic()) {
        AutoJob.schedulePeriodic();
        //Not related
    }
    if (DataMan.getInstance(getBaseContext()).getPureAutoLogin()) {
        startService(new Intent(this, AlwaysOnService.class));
    }
}

So my target was to Login into my isp automatically when I turn up my android’s WiFi, and the code works smooth. It doesn’t fail ever (It’s running for 7 hours and 37 minutes till now and working well | not across reboots).


To keep the receiver running across reboots, try the manifest registerable BOOT_COMPLETED action.

<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>

I also posted it at StackOverflow: https://stackoverflow.com/questions/50628102/broadcast-receiver-which-always-receives-broadcast-even-in-background-for-api/50628103#50628103

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.