Broadcastreceiver to obtain ServiceState information

放肆的年华 提交于 2019-12-30 22:55:21

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/4846271/broadcastreceiver-to-obtain-servicestate-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!