1 ImportAndroid.content.Context;2 ImportAndroid.net.ConnectivityManager;3 ImportAndroid.net.NetworkInfo;4 5 /**6 * Network Tool class Add network status license: <uses-permission7 * android:name= "Android.permission.ACCESS_NETWORK_STATE"/>8 * 9 * @authorBennyTen * One */ A Public classNetworkutil { - - /** the * Network Connection Status - * <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> - * @paramContext - * Program Context + * @returntrue: Can connect - */ + Public Static Booleanisnetworkconnected (Context context) { A if(Context! =NULL) { atConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); - -Networkinfo Networkinfo =connectivitymanager.getactivenetworkinfo (); - if(Networkinfo! =NULL) { - returnnetworkinfo.isavailable (); - } in } - return false; to } + - /** the * WiFi network connection Status * * <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> $ * @paramContextPanax Notoginseng * Program Context - * @returntrue: Can connect the */ + Public Static Booleaniswificonnected (Context context) { A if(Context! =NULL) { theConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); + -Networkinfo Networkinfo =Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); $ if(Networkinfo! =NULL) { $ returnnetworkinfo.isavailable (); - } - } the return false; - }Wuyi the /** - * Mobile Network connection Status Wu * <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> - * About * @paramContext $ * Program Context - * @returntrue: Can connect - */ - Public Static Booleanismobileconnected (Context context) { A if(Context! =NULL) { +Connectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); the -Networkinfo Networkinfo =connectivitymanager.getactivenetworkinfo (); $ if(Networkinfo! =NULL&& Networkinfo.gettype () = =connectivitymanager.type_mobile) { the returnnetworkinfo.isavailable (); the } the } the return false; - } in the /** the * Get network connection type About * @paramContext Program Contexts the * @returnconnectivitymanager.type_xxxx the */ the Public Static intGetconnectedtype (Context context) { + if(Context! =NULL) { -Connectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); theNetworkinfo Mnetworkinfo =connectivitymanager.getactivenetworkinfo ();Bayi if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) { the returnMnetworkinfo.gettype (); the } - } - return-1; the } the the}
Android to determine the network connection