Skip to content

Instantly share code, notes, and snippets.

@pranaypatel512
Last active July 8, 2016 09:39
Show Gist options
  • Save pranaypatel512/a8e560294fd8fd9431870b463dbace19 to your computer and use it in GitHub Desktop.
Save pranaypatel512/a8e560294fd8fd9431870b463dbace19 to your computer and use it in GitHub Desktop.
network utils Class of an application
package com.karconnect.app.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
public class NetworkUtils {
public static boolean isInternetAvailable(Context ctx) {
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
public static void showNetworkAlertDialog(Context mCtx) {
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
builder.setCancelable(false);
builder.setTitle("Connection");
builder.setMessage("No internet available");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment