Android Service Judge device networking status Detailed _android

Source: Internet
Author: User

First, to get whether the current Android device is networked, Android itself provides us with a service.

Private Connectivitymanager connectivitymanager;//used to determine whether there is a network 
 
Connectivitymanager = (Connectivitymanager) Getactivity (). Getsystemservice (Context.connectivity_service);//Get connection service for current network 
        

First we have to get to the Android Web service, and we 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"/>

Then we get the service of the system through Context.getsystemservice (Context.connectivity_service).

Then we use Android to provide us with this networkinfo class to put the system back to our network service information.

if (info = = null) {  //There is currently no active network connection (indicating that the user has turned off the data traffic service and that there are no data services to open WiFi) 
          toast.maketext (getactivity (), " Check that the network connection is turned on, 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 valid, if true means that the current Android phone is networked, possibly WiFi or GPRS, HSDPA, and so on. The detailed access method can be judged by the Getactivenetworkinfo () method of the Connectivitymanager class.

Connectivitymanager.getactivenetworkinfo (). isavailable ();

Above on the Android Service to judge the networking status of the data collation, follow-up continue to supplement the relevant information, thank you for your support to this site.

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.