开发者

To check wifi or 3G exist in android

开发者 https://www.devze.com 2023-01-20 03:20 出处:网络
Hello In my android application i am using the below code to make sure that network exists. But am receiving error at the very first line.Do i need to check any permission other than those listed.

Hello In my android application i am using the below code to make sure that network exists.

But am receiving error at the very first line.Do i need to check any permission other than those listed.

public static boolean haveInternet(Context ctx) { 

NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx 
        .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 

if (info == null || !info.isConnected()) { 
    return false; 
} 
if (info.isRoaming()) { 
    // here is the roaming option you can change it if you want to 
    // disable internet while roami开发者_C百科ng, just return false 
    return false; 
} 
return true; 

Permissions in manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission>   
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name= "android.permission.CHANGE_NETWORK_STATE"></uses-permission>


I am using

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

instead of

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

Try changing it.

0

精彩评论

暂无评论...
验证码 换一张
取 消