Broadcastreceiver for ServiceState Information

Does anyone know how to get phone service status (IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF) in Android.

I was hoping that a transsexual would be used to identify the changes, but I can’t find anything. I know there is a listener, but I'm not sure how to use it in my application, since it works as a service using WakefulIntentService (bycommonsguy).

With something like a battery level (i.e. BATTERY_LOW, BATTERY_OKAY) it's pretty simple, but I just can't work out similar things for changes in the phone service.

+4
source share
2 answers

Register receiver for

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

When you get the intention on your receiver, just use below a little hack from the 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 the source of the service state 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
source

You can write your own BroadcastReceiver. Your recipient will receive the connection changes and inform your desired instance of 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, your CommunicationManager instance that will be notified of connectivity:

 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 out NetworkInfo for more information on connection availability.

Remember to register ACCESS_NETWORK_STATE resolution in the manifest:

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

Hope this helps. Relations

0
source

Source: https://habr.com/ru/post/1337698/


All Articles