how to check internet in android code example
Example 1: Android check internet
public class InternetConnection {
public static boolean checkConnection(Context context) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr != null) {
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}
}
return false;
}
}
Example 2: Android check internet
new CheckNetworkConnection(this, new CheckNetworkConnection.OnConnectionCallback() {
@Override
public void onConnectionSuccess() {
Toast.makeText(context, "onSuccess()", toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFail(String msg) {
Toast.makeText(context, "onFail()", toast.LENGTH_SHORT).show();
}
}).execute();