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

1

8

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.

Micaelamicah answered 23/5, 2011 at 14:30 Comment(1)
undocumented but possibly intended as mentioned in this similar question (although i also have a hard time unterstanding the reasons behind this). and i can confirm this behavious exists in 2.2 and 2.3.x ...Bautista
P
8

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.

Parnas answered 29/9, 2011 at 10:29 Comment(1)
any solution? without using ConnectivityManager?Wedlock

© 2022 - 2024 — McMap. All rights reserved.