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

t.pateiro picture t.pateiro · May 23, 2011 · Viewed 9.7k times · Source

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.

Answer

Timmmm picture Timmmm · Sep 29, 2011