I have a method to detect connection status
boolean isInternetAvailable(WebView view) {
Boolean connected = false;
try {
ConnectivityManager connectivityManager = (ConnectivityManager) view.getContext()
.getSystemService(view.getContext().CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
Log.d("available", Boolean.toString(networkInfo.isAvailable()));
Log.d("connected", Boolean.toString(networkInfo.isConnected()));
Log.d("compare", Boolean.toString(networkInfo.getState() == NetworkInfo.State.CONNECTED));
return connected;
} catch (Exception e) {
}
return connected;
}
but when i disconnect all internet cables from my computer, and i can't load any page actually, emulator still thinks what he is connected and returns
11-21 13:43:22.103 20824-20824/lt.example.app D/available: true
11-21 13:43:22.103 20824-20824/lt.example.app D/connected: true
11-21 13:43:22.103 20824-20824/lt.example.app D/compare: true
to console, this method used in WebViewClient to detect connection while using WebView, what can be ?
The thing is, your emulator does not really care about your actual connection status on your machine. You probably need to enable/disable the airplane mode on the emulator to get correct results.
As long as your emulator has wifi/data enabled it will tell you yeah, i'm connected even if you can't reach the internet.
If you want to check for a working connection, please see: Android check internet connection ( isInternetAvailable()
)
Try creating new object:
boolean isInternetAvailable(WebView view) {
Boolean connected = false;
try {
ConnectivityManager connectivityManager = (ConnectivityManager) view.getContext()
.getSystemService(view.getContext().CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = new NetworkInfo();
networkInfo=connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
Log.d("available", Boolean.toString(networkInfo.isAvailable()));
Log.d("connected", Boolean.toString(networkInfo.isConnected()));
Log.d("compare", Boolean.toString(networkInfo.getState() == NetworkInfo.State.CONNECTED));
return connected;
} catch (Exception e) {
}
return connected;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.