I have this code to check for internet connection.
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
//if network is active and have internet connection
if(info != null && info.isConnected()==true){
//Some code
}
//if network is inactive or doesn't have internet connection
else if(info == null || info.isConnected()==false){
Context context = getApplicationContext();
CharSequence text = " Bad internet connection.";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
When I start the program,everything works properly with turn on internet connection,but when I pull out the internet cable from my router and in my app still have turn on wifi the app get true with this (if(info != null && info.isConnected()==true)) and crash.I don't know why this code get true.
Use this for check condition of network:
if (info!=null && info.isAvailable() && info.isConnected()) {
return true;
} else {
return false;
}
}
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.