ConnectivityManager getActiveNetworkInfo() is always null even with data traffic active

巧了我就是萌 提交于 2019-12-22 04:29:18

问题


i'm working on a android project and i had the need to check for internet connection. i searched the web and i found a solution here on stackoverflow. However, i'm having problems on checking the internet state. I already searched everywhere but i can't find any solution for my problem.

Here is the manifest:

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

Here is the code for checking if internet is connected:

cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo ni = cm.getActiveNetworkInfo();
isConnected = (ni != null && ni.isAvailable() && ni.isConnected()) ? true : false;
}

The problem is that even with TYPE_MOBILE data traffic active the ni variable is always null. To access the operators internet traffic is this the correct way of testing it? Or i have to use TelephonyManager? It's wierd because i used getNetworkInfo()[] and debugged it, the HSPA for TYPE_MOBILE appears there but isAvalaible is always false and getState() = DISCONNECTED. However i have operator's data traffic enabled and working (in other applications)

[EDIT]: by the way im testing it directly in the device not in emulator :).

Thanks in advance. Regards.


回答1:


There are various bugs in ConnectivityManager.

See http://code.google.com/p/android/issues/detail?id=11866 and http://code.google.com/p/android/issues/detail?id=11891 for example.



来源:https://stackoverflow.com/questions/6098639/connectivitymanager-getactivenetworkinfo-is-always-null-even-with-data-traffic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!