Android Judgment Web Link

Source: Internet
Author: User

Today in the judgment whether there is a network connection has been an error, and later learned that the lack of permissions.

Everyone knows that Android networking needs to add permissions:

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

But we also need to add access rights when we judge:

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

Or

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>

The idea of judgment is as follows:

First get the instance of network communication class

Connectivitymanagercwjmanager (Connectivitymanager) Getsystemservice (Context.connectivity_service);

Use Cwjmanager.getactivenetworkinfo (). isavailable (); To return is valid,

True indicates that the current Android phone is connected to the Internet, possibly WiFi or GPRS, HSDPA, etc.

The detailed access method can be judged by the Getactivenetworkinfo () method of the Connectivitymanager class.

The specific code is as follows:

Connectivitymanager Connectivitymanager = (Connectivitymanager) Getsystemservice (connectivity_service);         = Connectivitymanager.getactivenetworkinfo ();         if Null | | ! networkinfo.isavailable ())        {            // currently available network         }        else         {            //  No network currently available        }

Of course, you can also pass

Networkinfo[] info = Connectivity.getallnetworkinfo ();

The way, see more: Http://zhidao.baidu.com/link?url=tHa2_W1ykaRBPG_ 9931w57ywzmqmfdgpv9fhrftd8i5zsfcl8lblpz05vcfrwtlesvytkdquwoktd3f_9auzqy-ao4jbocoffw8biltmd9s

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.