4

Does anyone know of a way to obtain the phone service state (IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF) in android.

I was hoping there would be a broadcastreciever to identify the changes, but I can't find anything. I know there's a listener but I'm not sure how I would use that from my app as it runs as a service using a WakefulIntentService (by thecommonsguy).

With something like battery level (ie BATTERY_LOW, BATTERY_OKAY) it's quite easy, but I just can't work out a similar things for phone service changes.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Andrew
  • 41
  • 1
  • 2

2 Answers2

2

Register a receiver for

public static final String ACTION_SERVICE_STATE_CHANGED = "android.intent.action.SERVICE_STATE";

When you get intent on your receiver just use below little hack from android source

public void onReceive(Context context, Intent intent) {
    int state = intent.getExtras().getInt("state");
    if(state == ServiceState.STATE_IN_SERVICE)
    {
        //Do whatever you want
    }
    }

check source of service state class http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/telephony/ServiceState.java#ServiceState.setFromNotifierBundle%28android.os.Bundle%29

om252345
  • 2,395
  • 4
  • 29
  • 31
  • 2
    that piece of code does not work with API >= 4.3 because that extra isn't used anymore. If you check out http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/android/telephony/ServiceState.java#ServiceState.0mVoiceRegState , you won't find any variable being initialized from the "state" extra. Older versions do that. – Ricardo Belchior Jan 03 '14 at 15:17
  • 2
    It looks like for 4.3, they split the `state` key into `voiceRegState` and `dataRegState`. When in service, both values are 0 [`STATE_IN_SERVICE`] and in airplane mode, both values are 3 [`STATE_POWER_OFF`]. – Chris Feist Apr 17 '14 at 18:01
  • Could you tell me how did you find the name of that action? What did you google or where did you look? – pomber Jun 24 '14 at 03:49
0

You could write your own BroadcastReceiver. Your receiver will receive connectivity changes and inform your desired instance about the change (for example your own CommunicationManager):

public class ConnectivityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(getClass().getName(), "A change in network connectivity has occurred. Notifying communication manager for further action.");
    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if(info != null) {
        Log.v(getClass().getName(), "Reported connectivity status is " + info.getState() + ".");
    }
    CommunicationManager.updateConnectivityState(); // Notify connection manager
}

}

For example here your CommunicationManager instance, which will be notified about connectivity changes:

protected static void updateConnectivityState()
{
    boolean isConnected = false;
    if (_connec != null && (_connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(_connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ 
        isConnected = true;
        Log.i(CommunicationManager.class.getName(), "Device is connected to the network. Online mode is available.");
    }else if (_connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  _connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {             
        isConnected = false;
        Log.w(CommunicationManager.class.getName(), "Device is NOT connected to the network. Offline mode.");
    }
    _isConnected = isConnected;
}

Check the NetworkInfo class for further details about connectivity availability.

Don't forget to register the ACCESS_NETWORK_STATE permisson in your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

I hope this helps. Regards

saxos
  • 2,467
  • 1
  • 20
  • 21
  • 1
    Probably because ServiceState is not NetworkState. ServiceState is the state of the telephony radio whereas NetworkState will give you state of the data connexion (which may be wifi, wimax... i.e. NOT the telephony network) – Philippe Girolami Dec 17 '12 at 16:39