I am trying to use PhoneStateListener to handle connection state changes. I've registered the listener inside onCreate method of a service. The overriden method onDataConnectionStateChanged(int state) of the listener is being called when I enable or disable wifi connection from the phone' settings, but in both cases the 'state' param is 0 and it always enters the first case of the switch. Here's my implementation of the method:
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onDataConnectionStateChanged(int state) {
super.onDataConnectionStateChanged(state);
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
Toast.makeText(LocationService.this, "Data connection lost!", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.DATA_CONNECTED:
Toast.makeText(LocationService.this, "Data connection available!", Toast.LENGTH_LONG).show();
break;
}
}
};
The result is always a toast message saying: "Data connection lost!". I'm sure Im missing something simple here but I can't find out what the problem is.