javaandroidbackground-processnetworkinfo

NetworkInfo is not properly responding to network changes on phone running Nougat


Preface: This is working just fine on a phone running 6.0.1. but on my phone that is running 7.1.1, it is not, as shown below.

build.gradle (Module:app)

compileSdkVersion 25
buildToolsVersion "25.0.0"
applicationId "com.gesslar.threshvote"
minSdkVersion 19
targetSdkVersion 25

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <receiver
        android:name=".NetworkChangeReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </intent-filter>
    </receiver>

NetworkUtil.java I have a BroadcastReceiver that I use to simply call NetworkUtil.updateNetworkStatus(context);

public static void updateNetworkStatus(Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork == null) {
        Log.d(TAG, "NO CONNECTION INFORMATION");
        return;
    }
    if (activeNetwork.isConnected()) {
        Log.d(TAG, "WE ARE CONNECTED TO A NETWORK");
    } else {
        Log.d(TAG, "WE ARE NOT CONNECTED TO A NETWORK");
        return;
    }
    Log.d(TAG, "NETWORK TYPE: " + activeNetwork.getTypeName());
}

Starting with WiFi and Data Connected I have done the following tests Turn WiFi OFF

03-08 20:15:04.664 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION

Turn WiFi ON:

03-08 20:15:26.333 29796-29796/com.gesslar.threshvote D/NetworkUtil: WE ARE CONNECTED TO A NETWORK
03-08 20:15:26.333 29796-29796/com.gesslar.threshvote D/NetworkUtil: NETWORK TYPE: MOBILE

Turn data OFF:

No log line written

Turn data ON:

No log line written

Turn WiFi OFF and then turn data OFF:

1) 03-08 20:17:13.029 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION
2) No log line written

With both off, turn WiFi ON:

03-08 20:18:00.198 29796-29796/com.gesslar.threshvote D/NetworkUtil: NO CONNECTION INFORMATION
^ showed as soon as it started seeking for WiFi, but no further log line was written when WiFi became active.

I am absolutely bemused by the results. I saw somewhere else that I should bind it at runtime, however, my only activity is a Preferences activity. The app is not intended to have a runtime UI, rather using background IntentService and Notifications.


Solution

  • According to android docs,

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

    So what you need to do is start a Service, create a new instance of the Broadcast receiver NetworkChangeReceiver and register it to listen it to android.net.conn.CONNECTIVITY_CHANGE by using

    registerReceiver(new NetworkChangeReceiver(), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"))