Reputation: 33
I try code
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
It checks WiFi or Mobile network open and close only. But I want to check with Internet access. Such as open WiFi, but can connect the Internet.
Upvotes: 0
Views: 51
Reputation: 1836
I use a ping in Google to check the if the user have real internet connection, just be careful if your user is in China or any other country which Google is blocked.
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
Upvotes: 1