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
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
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