0

My app is a kiosk mode app (unless the user enters a code, he won't be able to access any Android apps). So, when the tab boots up, there are some settings to be done in a service. I am using the below code to check for network connectivity (this code is being reused as its part of utility class in my work). If there is network available, I need to execute a command, else an event has to be triggered.

ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();

In Android 7, this code works flawlessly. On boot up, since there is a SIM and network connectivity is available, I can execute the command.

But in Android 6, even when there is network availability, the code always returns not connected/OFFLINE. This happens on the device boot up. But, the connectivity returns true after the app launches.

Internally, the connectivityManager.activeNetworkInfo is based on a BroadcastReceiver with IntentFilter ConnectivityManager.CONNECTIVITY_ACTION. From the logs, I can see that the CONNECTIVITY_ACTION is being registered during the app installation, but this doesn't happen on device boot up.

I tried to make network connectivity check on the class which extends Application but that also is not working - thinking that check will make the app aware at start up that there is a network connection.

user198530
  • 331
  • 1
  • 3
  • 10
  • I would be really, really surprised if the issue had anything to do with your app launch. The call to connectivity manager calls through to another process to get result. It doesn't matter who is calling it. I suspect your problem is just that the particular device you are testing on takes longer for the network to connect. – Jeffrey Blattman May 31 '18 at 19:28
  • Thanks @JeffreyBlattman The devices I am trying on are identical Samsung Tab E, except for Android OS 6 and 7. – user198530 May 31 '18 at 20:33
  • I'd suggest getting more than 1 sample before declaring it's a 6 v. 7 issue. How did you determine the device isn't just taking longer to connect to the network? – Jeffrey Blattman May 31 '18 at 20:41

1 Answers1

1

To properly wait for network connectivity:

  1. Start your kiosk activity at boot with a boot receiver.
  2. When the app starts, register your receiver in the app, programmatically in your activity. Do not register it in the manifest. Manifest-registered receivers for CONNECTIVITY_ACTION are deprecated. If you are targeting API 22+, you can use ConnectivityManager.requestNetwork() with a callback instead of the broadcast (not described here).
  3. The return value of registerReceiver() is an intent which is the current state of the network (CONNECTIVITY_ACTION is a sticky broadcast). Act on that. If the intent has an extra EXTRA_NO_CONNECTIVITY then there's no network connection, and your app has to wait until the receiver is triggered, and then check again for connectivity.
  4. Remember that connectivity can go up and down any time so your receiver needs to trigger changes in your UI to this effect.

Different devices and different versions of Android and different network types (including different SIM cards) can cause differing delays as to when the network interface gets connected. It definitely is not related to your app starting, or having been started, etc. When you call getActiveNetworkInfo(), it's just a stub that performs an IPC to the system server to get the real value. There's no logic running or state kept in your process.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134