Sometimes doing Android development requires a network to connect to the server. Assume that there is no network to prompt. The code is very easy and the code is as follows:
public static Boolean isnetworkavailable (context context) {Connectivitymanager mgr = (connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo[] info = mgr.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length; i++) {if (info[i].gets Tate () = = NetworkInfo.State.CONNECTED) {return true;}}} return false;}
Can be used as a direct reference, such as
Isnetworkavailable (Mainactivity.this);
Don't forget to add permissions to the Androidmanifest.xml:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android: Name= "Android.permission.INTERNET"/>
Infer Mobile network Connection Status