Android Network Connection Tool
Public class NetUtil {/*** check the current network */public static boolean checkNet (Context context) {// determine whether the current user's mobile phone uses WIFI to communicate boolean isWIFI = isWIFIConnection (context); // determine whether the current user's mobile phone uses APN to communicate boolean isAPNConnection (context ); // No -- Prompt User -- jump to the network to set if (isAPN = false & isWIFI = false) {return false;} if (isAPN) {// Access Point // IP address is 10.0.0.172 port is 80 read from the mobile phone configuration information // some mobile phones: 010.000.000.172setAPN (context);} return true ;}/*** Read */private static void setAPN (Context context) {Uri PREFERRED_APN_URI = Uri from the phone configuration information. parse ("content: // telephony/carriers/preferapn"); ContentResolver contentResolver = context. getContentResolver (); Cursor query = contentResolver. query (PREFERRED_APN_URI, null, null); // obtain the information of the currently connected APN if (query! = Null & query. moveToNext () {// sets the ip address and port information GlobalParams. IP = query. getString (query. getColumnIndex ("proxy"); GlobalParams. PORT = query. getInt (query. getColumnIndex ("port") ;}}/*** determine whether WIFI is connected ** @ return */private static boolean isWIFIConnection (Context context) {ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager. getNet WorkInfo (ConnectivityManager. TYPE_WIFI); if (networkInfo! = Null) return networkInfo. isConnected (); return false;}/*** determine whether the APN is connected ** @ return */private static boolean isAPNConnection (Context context) {ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); if (networkInfo! = Null) return networkInfo. isConnected (); return false ;}}