I wanted to implement an app that prevents calls like a firewall. When I debug my app, I found that when there is a call in, the onCallStateChanged() function in interface PhoneStateListener is invoked three times. As a result, preventing one call can result in three logs. I’m so confused!!
my code:
@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);
PinkyJie,
onCallStateChanged() is working with BroadcastReceiver. so onCallStateChanged() will always get called whenever call state get changed. There are three call states.
TelephonyManager.CALL_STATE_IDLE: Phone is in idle mode, no call action.
TelephonyManager.CALL_STATE_OFFHOOK: At least one call is active in any kind.
TelephonyManager.CALL_STATE_RINGING: A new call arrived and is ringing or waiting.
TelephonyManager.
system will never go beyond that. please refer this link http://developer.android.com/reference/android/telephony/TelephonyManager.html