Android does not connect to the network every time it connects to the network. Therefore, you need to determine the network status during program startup. If there is no network, you are reminded to set the network status.
FirstTo determine the network status, you must have the corresponding permissions. The permission code (androidmanifest. XML) is as follows ):
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
ThenTo check whether the network status is available.
/*** Determine the network connection status * @ return true, available; false, unavailable */private Boolean isopennetwork () {connectivitymanager connmanager = (connectivitymanager) getsystemservice (context. connectivity_service); If (connmanager. getactivenetworkinfo ()! = NULL) {return connmanager. getactivenetworkinfo (). isavailable ();} return false ;}
LastIf not, enable network settings.
/*** To access Baidu homepage, you need to set */private void initmoregames () {string url_moregames = "http://www.baidu.com"; mwebview = (webview) findviewbyid (R. id. view_gamesort); If (mwebview! = NULL) {mwebview. requestfocus (); websettings = mwebview. getsettings (); If (websettings! = NULL) {websettings. setjavascriptenabled (true); websettings. setcachemode (mode_private); websettings. setdefaulttextencodingname ("UTF-8");} // determine whether the Network is available if (isopennetwork () = true) {mwebview. loadurl (url_moregames);} else {alertdialog. builder = new alertdialog. builder (moregamesactivity. this); builder. settitle ("no available network "). setmessage ("do you want to set the network? "); Builder. setpositivebutton ("yes", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {intent = NULL; try {string sdkversion = android. OS. build. version. SDK; If (integer. valueof (sdkversion)> 10) {intent = new intent (Android. provider. settings. action_wireless_settings);} else {intent = new intent (); componentname comp = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); intent. setcomponent (COMP); intent. setaction ("android. intent. action. view ");} moregamesactivity. this. startactivity (intent);} catch (exception e) {log. W (TAG, "Open Network Settings failed, please check... "); E. printstacktrace ();}}}). setnegativebutton ("no", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {dialog. cancel (); finish ();}}). show () ;}} else {log. W (TAG, "mwebview is null, please check... ");}}
Running interface:
Reference recommendations:
Android Network Connection judgment and handling
Android determines whether the network is enabled and opens the Network Setting interface.
Obtain the version number of the current Android system
How to determine the Android version
Android get version number (Baidu blog)