Android detects whether the current network is available
Source: Internet
Author: User
In Android Program The first step is to check whether the current network is available. If no network is available, exit the program if (isconnect (this) = false) {New alertdialog. builder (this ). settitle ("network error "). setmessage ("network connection failed, please confirm network connection "). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface arg0, int arg1) {// todo auto-generated method stub android. OS. process. killprocess (Android. OS. process. mypid (); system. exit (0 );}}). show ();} publi C static Boolean isconnect (context) {// obtain all connection management objects of the mobile phone (including management of Wi-Fi and net connections) Try {connectivitymanager connectivity = (connectivitymanager) context. getsystemservice (context. connectivity_service); If (connectivity! = NULL) {// obtain the network connection management object networkinfo info = connectivity. getactivenetworkinfo (); If (info! = NULL & info. isconnected () {// determine whether the current network is connected to If (info. getstate () = networkinfo. state. connected) {return true ;}}} catch (exception e) {// todo: handle exception log. V ("error", E. tostring ();} return false;} Add the permission <uses-Permission Android: Name = "android. permission. access_network_state "/>
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.