Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CONNECTIVITY_CHANGE Broadcast Android O

We have a wifi based application which has few millions of customer base. Core logic of the application is to connect the user to public open Hotspot with auto sign-in feature when connected. Application wholly depends on these broadcasts.

<intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>

Following broadcast, app will check connected wifi hotspot and try auto login.

In Android O, app doesn't receive any of the above mentioned broadcast. Looked into background service with broadcast receiver workaround and seems like android is stopping the service some time.

Any suggestion is well appreciated.

like image 491
Isham Avatar asked Mar 09 '23 06:03

Isham


2 Answers

Use JobScheduler (JobIntentService or Firebase Job Dispatcher for backward compatibility). Refer the latest video from Android Developers.

Using the method JobInfo.Builder.setRequiredNetworkType() you can schedule jobs to run when specific network conditions are met.

Quoting from this thread:

The network type can be one of three values:

  • JobInfo.NETWORK_TYPE_NONE: No network connectivity required.

  • JobInfo.NETWORK_TYPE_UNMETERED: An unmetered WiFi or Ethernet connection.

  • JobInfo.NETWORK_TYPE_ANY: Any network connection (WiFi or cellular).

JobInfo.NETWORK_TYPE_UNMETERED would suit your needs.

like image 64
Bob Avatar answered Mar 20 '23 02:03

Bob


Just register to the broadcastReceiver at runtime instead of in manifest.

If you need to do it in a service that always runs, do it in a foreground service.

Read here or here for more information, as apps targeting Android 7.0 and above won't get this intent anymore via the manifest:

Apps targeting Android 7.0 (API level 24) and higher do not receive this broadcast if they declare the broadcast receiver in their manifest. Apps will still receive broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

I've made an easy way to register at runtime, here.

like image 45
android developer Avatar answered Mar 20 '23 02:03

android developer