Создайте класс BroadcastReceiver и зарегистрируйте его внутри файла манифеста. И проверьте намерение исходящего вызова.
Ex:
public class DialCallListener extends BroadcastReceiver {
private static final String OUTGOING_CALL_INTENT = "android.intent.action.NEW_OUTGOING_CALL";
//Inside onReceive method, check the call state
@Override
public void onReceive(Context context, Intent intent) {
int callState = TelephonyManager.CALL_STATE_IDLE;
try {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
//Check if call state is OFFHook
if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
callState = TelephonyManager.CALL_STATE_OFFHOOK;
//Call the listener interface when call state change. And handle this state change inside PhoneListener class.
MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);
PhoneListener.onCallStateChanged(callState, number);
}
}catch (Exception err) {
err.printStackTrace();
}
}}
// Теперь создаем слушателя, если состояние вызова изменилось (на наборе любого номера)
private class MyPhoneStateListener extends PhoneStateListener implements ICallReceiver {
public void onCallStateChanged(int state, String dialing_number) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
//Handle Call dial
mPresenter.handleCallOffHook(incomingNumber);
break;
}
}}
Может быть вы можете проверить мой код [здесь] (https://github.com/xdtianyu/CallerInfo/blob/ мастер/приложение/SRC/Основной/Java/орг/xdty/callerinfo/приемник/IncomingCall.java # L46) – tianyu