Android detects network status to determine if the current network is available

Source: Internet
Author: User

The user's current network is available: WiFi, 2g/3g Network, users open and not open the network, and whether can be used is not the same thing. Can use refers to: users open the network and can connect to the Internet for Internet access.

To detect if the current network is available, the code is as follows:

/*** Detect when the network (WLAN, 3g/2g) status *@paramContext Context *@returnTrue indicates that the network is available*/     Public Static Booleanisnetworkavailable (Context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Connectivity! =NULL) {Networkinfo info=Connectivity.getactivenetworkinfo (); if(Info! =NULL&&info.isconnected ()) {                //the current network is connected                if(info.getstate () = =NetworkInfo.State.CONNECTED) {//the network currently connected is available                    return true; }            }        }        return false; }

Permissions added in the Androidmanifest.xml file:

  <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>  <uses-permission Android : Name= "Android.permission.INTERNET"/>

http://blog.csdn.net/android_ls/article/details/8605991

Android detects network status and determines if the current network is available

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.