Search code examples
androidcalldefaultandroid-dialer

Default phone call - How to select a number service during a call?


I am making a default phone call. Everything works well till I made a call to the switchboard operator.

In this kind of call, the phone says: "Press 1 to do A, press 2 to do B".

I did some research for hours but couldn't find one... I did try this code, but it doesn't work.

 keyPressed(KeyEvent.KEYCODE_1); // when press key 1

 private void keyPressed(int keyCode) {
    ....
    Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel://" + keyCode));
    startActivity(I);
    ....
    playTone(ToneGenerator.TONE_DTMF_1, TONE_LENGTH_INFINITE);
 }

Big thanks for any of your suggestions! enter image description here

Added 1: I am using InCallService like this:

 class CallService : InCallService() {
 private var isShowEnded = true

override fun onCallAdded(call: Call) {
    super.onCallAdded(call)
    OngoingCall().setCall(call)
    CallActivity.getInstance().start(this, call)
    isShowEnded = false
}

override fun onCallRemoved(call: Call) {
    super.onCallRemoved(call)
    OngoingCall().setCall(null)
}
 }

and OngoingCall:

public class OngoingCall {

public static BehaviorSubject<Integer> state = BehaviorSubject.create();
private static Call sCall;

public Call getsCall() {
    return sCall;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private Object callback = new Call.Callback() {
    @Override
    public void onStateChanged(Call call, int newState) {
        super.onStateChanged(call, newState);
        state.onNext(newState);
    }
};

@RequiresApi(api = Build.VERSION_CODES.M)
public final void setCall(@Nullable Call value) {
    if (sCall != null) {
        sCall.unregisterCallback((Call.Callback) callback);
    }

    if (value != null) {
        value.registerCallback((Call.Callback) callback);
        state.onNext(value.getState());
    }

    sCall = value;
}

@RequiresApi(api = Build.VERSION_CODES.M)
public void answer() {
    if (sCall != null) {
        assert sCall != null;
        sCall.answer(VideoProfile.STATE_AUDIO_ONLY);
    }
}

@RequiresApi(api = Build.VERSION_CODES.M)
public void hold(boolean hold) {
    if (sCall != null) {
        if (hold) sCall.hold();
        else sCall.unhold();
    }
}

@RequiresApi(api = Build.VERSION_CODES.M)
public void addCall(Call call) {
    if (sCall != null) {
        sCall.conference(call);
    }
}

@RequiresApi(api = Build.VERSION_CODES.M)
public void hangup() {
    if (sCall != null) {
        sCall.disconnect();
    }
}
}

And then I tried this when pressing keyboard:

      mTrueCallerOngoingCall.getsCall().playDtmfTone((char) tone); // inside playTone()

But it's still not working :(

Update 2: I have fixed my adding this method:

private char getChar(int tone) {
    if (tone == 0) return '0';
    else if (tone == 1) return '1';
    else if (tone == 2) return '2';
    else if (tone == 3) return '3';
    else if (tone == 4) return '4';
    else if (tone == 5) return '5';
    else if (tone == 6) return '6';
    else if (tone == 7) return '7';
    else if (tone == 8) return '8';
    else if (tone == 9) return '9';
    else if (tone == 10) return '*';
    else return '#';
}

and change from my above code to

            mTrueCallerOngoingCall.getsCall().playDtmfTone(getChar(tone));
            mTrueCallerOngoingCall.getsCall().stopDtmfTone();

Solution

  • It is .Hope can help you.

    call.playDtmfTone(char);
    

    The call PATH:

    android.telecom.Call;
    

    From: Any Class extends InCallService.

    In Method: onCallAdded(call);