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