I use the isconnectInternet()
method to control the mobile phone internet connection in my all of classes. I do that, but it is duplicated in every class. How I can write it once and use it everywhere in the project?
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
} else
return false;
}
This is my method to control the internet
if(isConnected){
if there is internet conn.
}
else{
there is no internet do samething
}