Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Created May 8, 2014 18:36
Show Gist options
  • Save Kursulla/38c0ea8c955fa3b206f0 to your computer and use it in GitHub Desktop.
Save Kursulla/38c0ea8c955fa3b206f0 to your computer and use it in GitHub Desktop.
package rs.webnet.locmar.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
/**
* Created by kursulla on 2/10/14.
* <p/>
* Class purpose:
*/
public class InternetConnectionUtil {
public static boolean hasAnyConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
return true;
}
return false;
}
/**
* Check does device has mobile (GPRS, 3G...) internet connection;
*
* @param context
*
* @return
*/
public static boolean hasMobileConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnectedOrConnecting()) {
return true;
}
return false;
}
/**
* Check does device has mobile (GPRS, 3G...) internet connection, BUT not if device is in reaming;
*
* @param context
*
* @return
*/
public static boolean hasMobileConnectionWithoutRoaming(Context context) {
if (hasMobileConnection(context) == false) {
return false;
}
if (isInRoaming(context)) {
return false;
}
return true;
}
/**
* Check does device has WiFi internet connection;
*
* @param context
*
* @return
*/
public static boolean hasWifiConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
BLog.d("ddddd", "wifiNetwork != null->" + (wifiNetwork != null) + " wifiNetwork.isConnectedOrConnecting()->" + (wifiNetwork.isConnectedOrConnecting()));
if (wifiNetwork != null && wifiNetwork.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
/**
* Check is device in roaming;
*
* @param context
*
* @return
*/
public static boolean isInRoaming(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork.isRoaming();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment