There are many applications in Android programming need to determine whether there is a network problem, now I would like to say about the network, the source code is as follows:.java /** * Check that the current network is available * * @param context * @return */ public boolean isnetworkavailable (activity activity) { context context = activity.getapplicationcontext (); // get phone all connection management objects (including management of connections such as Wi-fi,net) ConnectivityManager connectivityManager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); if (connectivitymanager == null) &nbsP { return false; } else { // Get Networkinfo Objects NetworkInfo[] Networkinfo = connectivitymanager.getallnetworkinfo (); if (networkinfo != null && networkinfo.length > 0) { for (int i = 0; i < networkinfo.length; i++) { System.out.println (i + "= = = Status = = =" + networkinfo[i].getstate ()); System.out.println (i + "= = = Type = = =" + networkinfo[i].gettypename ()); // Determine if the current network state is a connection state if (Networkinfo[i].getstate () == networkinfo.state.connected) { &nbSp; return true; } } } } return false; }
This method is judged in the OnCreate () method, with the following code:. java@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); if (isnetworkavailable (maintivity.this) { Toast.maketext (This, "there are currently available networks!") ", Toast.length_long). Show ();} else{Toast.maketext (This, "No network at this moment!" ", Toast.length_long). Show ();}} Additionally, you need to add permissions <!--allow apps to be networked--<uses-permission android:name= "Android.permission.INTERNET"/> <!--allow App Check network status--<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission>
This article is from the "Android Network" blog, so be sure to keep this source http://3127279300.blog.51cto.com/10023287/1725343
Android about judging if the app has a network