3

I am trying to figure out how do I detect wifi state change while my app is in the background. To give a summary of my issue. In my activity I register a receiver OnStart()such that :

IntentFilter networkIntent = new IntentFilter();
        networkIntent.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkIntent.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        registerReceiver(wifichangereceiver, networkIntent);

and then I define my wifichangereceiver fn :

 public final BroadcastReceiver wifichangereceiver = new BroadcastReceiver() {


        @Override
        public void onReceive(final Context context, final Intent intent) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (wifi.isConnected()) {
                isConnectedtoWifi = true;


                if (MyCallisInConnectedState()) {
                    onNetworkStateChanged(true);
                }

            } else {
                isConnectedtoWifi = false;

            }
        }
    };

and I unregister it in Onstop() :

unregisterReceiver(wifichangereceiver);

However, this mechanism works ONLY when the app is in foreground since I am registering and unregistering in my activity itself. Is there any easier way to monitor the wifi status when the call is in background aswell (or possibly thoughout? ) I tried adding a receiver in manifest but not sure if that is the correct approach. I want to be able to tell when my wifi state changes.Any ideas?

  • [Use a Service](https://developer.android.com/guide/components/services) – TheWanderer Sep 11 '18 at 23:48
  • is there a way without using a service and by modifying my code it'll be possible in an easier way? –  Sep 11 '18 at 23:49
  • No, you have to use a Service for background logic. It's the whole reason Services exist. – TheWanderer Sep 11 '18 at 23:53
  • Actually I just want to check if the wifi state is changed or not. If the wifi state is not changed I dont want the broadcast receiver to be triggered, based on my above code, I tried filtering out to check if the wifi state has changed to make sure I am still connected to wifi and nothing has changed, but as soon as it goes to the foreground it triggers wifi changed warning even though nothing was changed. Any idea why it maybe doing this? –  Sep 11 '18 at 23:55
  • Use `JobScheduler` and `JobService` – olajide Sep 11 '18 at 23:57

2 Answers2

0

You can use JobScheduler and JobService for Connection Changes.

checkout this answer

https://stackoverflow.com/a/48658403/4564200

https://stackoverflow.com/a/37289411/4564200

olajide
  • 972
  • 1
  • 13
  • 26
0

If your app targets API level 26 or higher, you've to use a foreground service to do any long-running task even when the user isn't interacting with the app. Since Android Oreo, The system imposes restrictions on running background services when the app itself isn't in the foreground. For performing short living tasks in the background, your app should use a scheduled job instead.

You may refer this training doc to understand how it works for your requirement.

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73