Android detects network connection status

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.