2

Faced a weird behaviour of NetworkInfo.isConnectedOrConnecting() when it is called from Service. It simply returns false although phone is connected to the Network. From Activities and Fragments this snippet works as expected.

public boolean isOnline() {
    if (mContext == null) {
        return false;
    }

    NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

Anyone faced this issue? Perhaps there's another method for checking network connection inside Services.

AnZ
  • 1,040
  • 24
  • 54
  • Please add device model and android version. – Chris Gomez Sep 29 '17 at 14:32
  • @cgomezmendez, tried on phone Nexus 5x Android 8.0 and emulator Nexus 5 Android 6.0 – AnZ Sep 29 '17 at 14:41
  • it's returning false because netinfo == null or because netInfo.isConnectedOrConnecting is false? – Chris Gomez Sep 29 '17 at 14:43
  • 1
    @cgomezmendez because `netInfo.isConnectedOrConnecting is false` – AnZ Sep 29 '17 at 15:04
  • @AnZ I'm facing the same issue. From background service, the network condition always returns false. Have you found the cause of it? – Dharmendra Oct 15 '18 at 08:20
  • @Dharmendra reason is mentioned in answer below. Although it's not the best solution. If you need to make API call from service it way more safer to use job manager, timeouts or rx. It will handle case when internet is not available properly – AnZ Oct 18 '18 at 10:22

2 Answers2

1

Try something like this in your service class.

public boolean isOnline(Context context) {
    ConnectivityManager manager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null) {
        return false;
    }
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null){
        if (networkInfo.isConnectedOrConnecting()) {
            return true;
        }
    }
    return false;
}

Edit: using a context singleton :

    public class ServiceContextManager {
    private static ServiceContextManager instance;

    public static ServiceContextManager getInstance(Context context) {
        if (instance == null) {
            instance = new ServiceContextManager(context.getApplicationContext());
        }

        return instance;
    }

    private Context mContext;

    private ServiceContextManager(Context context) {
        mContext = context;
    }
}
codeFreak
  • 353
  • 1
  • 3
  • 15
  • The problem here is we don’t know where that context came from, and it is not safe to hold a reference to the object especially on a service. Use a singleton. – codeFreak Sep 29 '17 at 15:15
1

I was using the exact code and was facing the same issue till yesterday when I found something better. It may look like different but this really works great as it also checks whether there is working internet connection or not as isNetworkConnected() will return true in case of a connected wifi with no internet so you can use this code.

public boolean isOnline() throws InterruptedException, IOException
    {
        String command = "ping -c 1 google.com";
        return (Runtime.getRuntime().exec (command).waitFor() == 0);
    }

You can also change google.com to any site as google.com might be down in some countries.
I was using this in an activity not as service but it will work.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • This solution works but it freezes the thread. Sometimes for too long.. – AnZ Oct 02 '17 at 07:53
  • @AnZ I'm using it and sometimes the connection can be too slow but didn't face any issue. How can you be so sure that the freezing is because of this?. – Lalit Fauzdar Oct 02 '17 at 10:04
  • It may be appliable for some cases which doesn't rely on execution speed. But that's not my case. The solution is good in terms of alternative. – AnZ Oct 02 '17 at 10:37
  • Yes, it is an alternative but it works well. I'm using it for google maps API to check for active internet connection and it executes fast and processes the data fast. I really didn't face any issue for speed. – Lalit Fauzdar Oct 02 '17 at 10:41