Network status always returns true for isConnected () and isConnectedOrConnecting ()

My application needs to know if it has internet access or not. I tried to follow the best practice on StackOverflow question ... However, the following method always returns true:

public static boolean isOnline(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();

}

      

A similar question was asked earlier Network status is always true for Android. How? However, the proposed solution uses legacy methods and shouldn't be a solution anyway, I suppose.

I am running code on an emulator that runs on my development machine (my laptop) and I test my code using a few conditions:

  • disable WIFI on laptop (and without cable)
  • enable airplane mode on the laptop.
  • use adb shell svc wifi enable / disable

    for direct work with the emulator.

No matter what I do, it always returns true and gives me a timeout when I actually try to connect to the www resource (so there is no magic internet connection):

String searchResultsStr = IOUtils.toString(url.openStream());

      

Help someone?

+3
java android android-emulator networking android-networking


source to share


No one has answered this question yet

See similar questions:

617
How to check Internet access on Android? InetAddress never runs out
0
The network state is always correct for Android. How?

or similar:

2248
Is a finally block always executable in Java?
935
Eclipse Cannot Start - Java was started but exit code returned = 13
559
Android Network Libraries Comparison: OkHTTP, Retrofit and Volleyball
63
How can I tell if Mobile Data is enabled (even when connected via WiFi)?
44
Windows Phone 8 emulator cannot connect to the Internet
4
Android does not detect data connections
1
Mi Devices blocking internet access when the app is in the background
1
why does Windows Phone 7 NetworkInterface.GetIsNetworkAvailable () return true when airplane mode is enabled
0
I am trying to get the internet connection status in Android but I cannot get it right
0
How to check Internet access on Android



All Articles
Loading...
X
Show
Funny
Dev
Pics