Android determines whether the current Android device is connected

Source: Internet
Author: User

first of all, to get the current Android device is connected, then Android itself provides us with a service

Private Connectivitymanager connectivitymanager;//is used to determine if there is a network Connectivitymanager = (Connectivitymanager) getActivity ( ). Getsystemservice (Context.connectivity_service);//Gets the connection service for the current network Networkinfo info = Connectivitymanager.getac Tivenetworkinfo (); Get the active network connection information


first we have to get the Android Network Service, need to add a permission to the configuration file

<!--get information about whether a user's phone is networked--><uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>


and then we passed Context.getsystemservice(context.connectivity_service) Get the system service

then we use Android to provide us with this Networkinfo class to go to the system return to our network service information

if (info = = null) {//does not currently have an activated network connection (indicating that the user has turned off the data traffic service, or turned on wifi and other data Services) Toast.maketext (Getactivity (), "Check the network                                    ", Toast.length_short). Show (); } else {//There is currently an active network connection}


Of course, we can also use this code to determine whether our network connection is available

To return is valid, if True indicates that the current Android phone is already networked, possibly WiFi or GPRS, HSDPA, and so on. Specifically, the Getactivenetworkinfo () method of the Connectivitymanager class can be used to determine the detailed access Method Connectivitymanager.getactivenetworkinfo (). IsAvailable ();


of course, after the development APP It is also necessary to conduct a full range of tests: www.ineice.com


Android determines whether the current Android device is connected

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.