Running the first step in the Android program is to detect if there is currently a network available and exit the program if no network is available
if(Isconnect ( This)==false) { NewAlertdialog.builder ( This). Settitle ("Network Error"). Setmessage ("Network connection failed, please confirm network connection"). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) { //TODO auto-generated Method Stubandroid.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (0); }}). Show (); } Public Static BooleanIsconnect (Context context) {//Get all connection management objects for your phone (including management of connections such as Wi-fi,net) Try{Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Connectivity! =NULL) { //get the objects for network connection managementNetworkinfo info =Connectivity.getactivenetworkinfo (); if(Info! =NULL&&info.isconnected ()) { //determine if the current network is connected if(info.getstate () = =NetworkInfo.State.CONNECTED) {return true; } } } } Catch(Exception e) {//Todo:handle ExceptionLOG.V ("Error", e.tostring ()); } return false; }
The last point has to be manifest. Add permissions
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
Android detects if the current network is available