ConnectivityManager getActiveNetworkInfo()总是空,即使数据流量活跃 [英] ConnectivityManager getActiveNetworkInfo() is always null even with data traffic active
问题描述
我工作的一个Android项目,我不得不需要检查互联网连接。我在网上搜索,我找到了一个解决方案,在这里计算器。不过,我有在检查网络状态的问题。我已经到处找,但我无法找到我的问题的任何解决方案。
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.
下面是明显的:
<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" />
下面是$ C $下,如果互联网连接检查:
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;
}
的问题是,即使采用TYPE_MOBILE数据业务活性的妮变量总是空。要访问运营商的互联网流量是这样测试它的正确方法是什么?或者我必须使用TelephonyManager? 这是奇怪的,因为我用getNetworkInfo()[]和调试吧,HSPA为TYPE_MOBILE在那里出现,但isAvalaible永远是假的,的getState()=断开。但是我有运营商的数据流量启用并正常工作(在其他应用程序)
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)
:通过即时通讯方式,直接在设备测试它不是在模拟器:)
: by the way im testing it directly in the device not in emulator :).
在此先感谢。问候。
推荐答案
有在 ConnectivityManager各种错误
。
请参阅<一href="http://$c$c.google.com/p/android/issues/detail?id=11866">http://$c$c.google.com/p/android/issues/detail?id=11866和<一href="http://$c$c.google.com/p/android/issues/detail?id=11891">http://$c$c.google.com/p/android/issues/detail?id=11891例如,
这篇关于ConnectivityManager getActiveNetworkInfo()总是空,即使数据流量活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!