Why is onCallStateChanged () on Android called more than once at a time?

I wanted to implement an application that prevents calls such as a firewall. When I debug my application, I find that when I call the onCallStateChanged () function in the PhoneStateListener interface is called three times. As a result, preventing one call can result in three logs. I'm so confused !!

my code is:

@Override public void onCallStateChanged(int state, String incomingNumber) { try { if (state == TelephonyManager.CALL_STATE_RINGING && PhoneUtil.getITelephony(tpm).isRinging()) { String flag = isBlockCall(myContext, myHelper, myTypes, incomingNumber); if (flag.length() > 0) { blockCall(); myHelper.insertLog(new String[] { flag, incomingNumber, String.valueOf(System.currentTimeMillis()), null }); showNotification(myContext, incomingNumber, System.currentTimeMillis()); } } } catch (Exception e) { e.printStackTrace(); } super.onCallStateChanged(state, incomingNumber); } }, PhoneStateListener.LISTEN_CALL_STATE); 
+4
source share
5 answers

PinkyJie,

onCallStateChanged () works with BroadcastReceiver. therefore, onCallStateChanged () will always be called whenever the call state changes. There are three call states.

TelephonyManager.CALL_STATE_IDLE: the phone is in standby mode, there is no call action.
TelephonyManager.CALL_STATE_OFFHOOK: at least one call is active in any form.
TelephonyManager.CALL_STATE_RINGING: A new call has arrived and is ringing or waiting. TelephonyManager

the system will never go beyond that. please refer to this link http://developer.android.com/reference/android/telephony/TelephonyManager.html

 public class TestPhoneStateListener extends PhoneStateListener{ public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub System.out.println("aaaaaaaaaaaaaaa----------------------------------"+state); try { if (state == TelephonyManager.CALL_STATE_RINGING ) { } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } super.onCallStateChanged(state, incomingNumber); } } 
0
source

The answer lies in your code where you create the TelephonyManager and then register the listener with it. It will be registered for the listener every time you start a new call, and therefore for several listeners connected to the same telephony manager, the result is onCallStateChanged () for each listener.
try creating a new Telephony Manager and registering it where it runs only once. (Constructor).

TelephonyManager tmanager = (TelephonyManager) this.getSystemService (TELEPHONY_SERVICE);

tmanager.listen (new CallListener (), PhoneStateListener.LISTEN_CALL_STATE);

+11
source

To better express Akashโ€™s answer, onCallStateChanged() is called several times because TelephonyManager.listen() is inside onReceive() . Thus, a new PhoneStateListener is created and registered with TelephonyManager whenever an event is broadcast, which is why many of them.

I personally did something similar in my code:

 if (noCallListenerYet) { // noCallListenerYet is static boolean TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); tm.listen(new OutgoingCallListener(), PhoneStateListener.LISTEN_CALL_STATE); noCallListenerYet = false; } 
+8
source

Well, now I was suffering from the same thing, but for some reason I found a way to do this.

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; public class CallActionsReceiver extends BroadcastReceiver { static ThePhoneStateListener phoneStateListener; @Override public void onReceive(Context arg0, Intent arg1) { TelephonyManager manager = (TelephonyManager) arg0 .getSystemService(arg0.TELEPHONY_SERVICE); if (phoneStateListener == null) { phoneStateListener = new ThePhoneStateListener(arg0); manager.listen(phoneStateListener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE); } } } 

Thus, TelephonyManager will listen only once. Greetings.,

+6
source

According to official docs

A callback that is called when a device's call state changes.

  • This means that whenever an incoming call rings on your phone, it will change state.
  • An incoming call is rejected, it will also cause a state change.
  • Incoming call taken, state changes!
  • Finally, no action will also lead to a change in state

They say that after a state change it makes sense to see how they shot twice at any resulting reaction.

0
source

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


All Articles