I need an application wide listener to determine when the service state changes on the phone. I’ve been through every example online, copied line for line, modified the manifest, and I CANNOT get anything to work.
Does a phone state listener HAVE to been in activity? Can I have some plain old class to listen for a phone state change??
This is very frustrating.
In my class I have…
private class ServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(android.content.Context context, Intent intent) {
Log.i(TAG, "onReceive");
MyPhoneStateListener qpsl = new myPhoneStateListener();
TelephonyManager tm = (TelephonyManager)context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
tm.listen(qpsl, PhoneStateListener.LISTEN_SERVICE_STATE);
}
}
private class MyPhoneStateListener extends PhoneStateListener{
public void onServiceStateStateChanged(ServiceState serviceState){
Log.i(TAG, "ServiceState = " + serviceState.getState();
}
I know I need to modify the manifest for the broadcast receiver and phonestate. Where? I want this application wide.
This is all the code I have, can someone show me what I’m missing so that I can listen for a service state change?
I suspect the reason nothing works for you is the broadcast is never received. What broadcast have you registered in manifest? Remember, phone state change is not a “traditional” broadcast. You need to register this
PhoneStateListenerobject instead.In any case, you don’t need a
BroadcastReceiverfor this. Just use aServiceinstead. Just move the code fromonReceive()above intoonStartCommand()of aServiceand you should be good to go.I have implemented something along similar lines in a simple “Poor Signal History” app and it works fine. The project is uploaded here.