Skip to content Skip to sidebar Skip to footer

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:

publicclassServiceReceiverextendsBroadcastReceiver {
    static CallStateListener phoneListener;

    @OverridepublicvoidonReceive(final Context context, Intent intent) {

        if (phoneListener == null) {
            phoneListener = newCallStateListener();
            TelephonyManagertm= (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"