Broadcastreceiver to obtain ServiceState information

后端 未结 2 601
一个人的身影
一个人的身影 2021-01-19 05:14

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 broadc

2条回答
  •  花落未央
    2021-01-19 05:37

    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

提交回复
热议问题