tags:

views:

171

answers:

2

Following line will only not result in a nullpointer exception if I'm connected to a WLAN:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Otherwise I get a nullpointer-exception straight ahead? How can I fix this? I want my background service to only work, when it is connected to wlan. But that Nullpointerexception just kills the whole app...

A: 

Add to the manifest the following line:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Macarse
I already did that. Still won't change.
A: 
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null && netInfo.getType() == 1) {
 // CONNECTION_WIFI = true;
}
embo