Android-determine the network status
Android mobile phones support both WIFI and 3G, determine the network connection, and determine the network type (WIFI or 3G ).
Permission:
Determine network connection
/*** Determine whether the Network is available. This method is used to check network requests in advance to save resources. ** @ return available status */public static boolean isNetAvailable () {Context context = App. getAppContext (); ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo = manager. getActiveNetworkInfo (); return (info! = Null & info. isAvailable () & info. isConnected ());}
Determine the network type
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);// 3GState mobile = conMan.getNetworkInfo(0).getState();// WIFIState wifi = conMan.getNetworkInfo(1).getState();if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) { // TODO: in mobile do something} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) { // TODO: in wifi do something}
More refined network types:
TelephonyManager # getNetworkType or NetworkInfo # getSubtypeName