TelephonyManager.CALL_STATE_RINGING Calls Twice While One Call Ringing
I use descendant - class of PhoneStateListener: class CallStateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) {
Solution 1:
When I tried your code, it run 10 times instead of running just once! It is better to declare phoneListener as a static variable in this case. That way you can guarantee that listen method will not be called more than once. Check this out:
public class ServiceReceiver extends BroadcastReceiver {
static CallStateListener phoneListener;
@Override
public void onReceive(final Context context, Intent intent) {
if (phoneListener == null) {
phoneListener = new CallStateListener();
TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
tm.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
Also, I have changed the way you deal with TelephonyManager - I couldn't find that TelephonyManagerSingletone that you mentioned in your code.
I hope this helps somebody!
Solution 2:
Not sure if it's a bug or a feature, but since API19 that happens with broadcasts.
For those who will search for the solution - you can keep track of previous state. Do stuff only if it is different.
Post a Comment for "TelephonyManager.CALL_STATE_RINGING Calls Twice While One Call Ringing"