Created
August 5, 2015 20:50
-
-
Save IvanWooll/5a75ef477e860e2d303d to your computer and use it in GitHub Desktop.
Android utility class for checking device's network connectivity and type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package theappdevelopers.networkutilstest.utils; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.telephony.TelephonyManager; | |
/** | |
* Created by ivanwooll on 05/08/15. | |
*/ | |
public class NetworkStateProvider { | |
public static final int STATE_DISCONNECTED = 0; | |
public static final int STATE_WIFI = 1; | |
public static final int STATE_4G = 2; | |
public static final int STATE_3G = 3; | |
public static final int STATE_LEGACY = 4; | |
public static final int STATE_UNKNOWN = 5; | |
public static int getNetworkState(Context context) { | |
if (!isConnected(context)) { | |
return STATE_DISCONNECTED; | |
} | |
if (isConnectedWifi(context)) { | |
return STATE_WIFI; | |
} | |
if (isFourGeeConnected(context)) { | |
return STATE_4G; | |
} | |
if (isThreeGeeConnected(context)) { | |
return STATE_3G; | |
} | |
if (isLegacyConnected(context)) { | |
return STATE_LEGACY; | |
} | |
return STATE_UNKNOWN; | |
} | |
public static boolean isConnected(Context context) { | |
NetworkInfo info = NetworkStateProvider.getNetworkInfo(context); | |
return (info != null && info.isConnected()); | |
} | |
public static NetworkInfo getNetworkInfo(Context context) { | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
return cm.getActiveNetworkInfo(); | |
} | |
public static boolean isConnectedWifi(Context context) { | |
NetworkInfo info = NetworkStateProvider.getNetworkInfo(context); | |
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI); | |
} | |
public static boolean isFourGeeConnected(Context context) { | |
NetworkInfo info = NetworkStateProvider.getNetworkInfo(context); | |
return info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE; | |
} | |
public static boolean isThreeGeeConnected(Context context) { | |
NetworkInfo info = NetworkStateProvider.getNetworkInfo(context); | |
switch (info.getSubtype()) { | |
case TelephonyManager.NETWORK_TYPE_HSPAP: | |
case TelephonyManager.NETWORK_TYPE_UMTS: | |
case TelephonyManager.NETWORK_TYPE_HSUPA: | |
case TelephonyManager.NETWORK_TYPE_HSDPA: | |
return true; | |
default: | |
return false; | |
} | |
} | |
public static boolean isLegacyConnected(Context context) { | |
NetworkInfo info = NetworkStateProvider.getNetworkInfo(context); | |
switch (info.getSubtype()) { | |
case TelephonyManager.NETWORK_TYPE_EDGE: | |
case TelephonyManager.NETWORK_TYPE_GPRS: | |
case TelephonyManager.NETWORK_TYPE_1xRTT: | |
case TelephonyManager.NETWORK_TYPE_CDMA: | |
case TelephonyManager.NETWORK_TYPE_EHRPD: | |
case TelephonyManager.NETWORK_TYPE_EVDO_0: | |
case TelephonyManager.NETWORK_TYPE_EVDO_A: | |
case TelephonyManager.NETWORK_TYPE_EVDO_B: | |
case TelephonyManager.NETWORK_TYPE_IDEN: | |
return true; | |
default: | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment