Check Internet Newtwork Connection

Function for check internet connection

/**
* @param context
* @param message
* @return
* @author Mihir
* @description use to check internet newtwork connection
* if network connection not available than alert for open network settings
*/
public static boolean isOnline(final Context context, boolean message) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
if (netInfo != null) {
if (netInfo.isConnectedOrConnecting()) {
return true;
}
}
if (message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Please check your network connection");
builder.setCancelable(false);
builder.setPositiveButton("Network Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton("Wifi Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
context.startActivity(new Intent(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)));
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
return false;
}
view raw Utils.java hosted with ❤ by GitHub
if(Common.isOnline(Category.this, true)) {
// put your code here
}
view raw YourClass.java hosted with ❤ by GitHub

Comments

Popular Posts