1

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();
Stevie
  • 401
  • 4
  • 13

1 Answers1

2

It is .Hope can help you.

call.playDtmfTone(char);

The call PATH:

android.telecom.Call;

From: Any Class extends InCallService.

In Method: onCallAdded(call);

lantian
  • 176
  • 7
  • It's seem like what I need.. But it didn't work. I have added some more detail in my post. Please let me know if I did something wrong. Big thanks – Stevie May 26 '20 at 08:54
  • 1
    call.playDtmfTone(char). And then invoke the call.stopDtmfTone(); for a tag. the tone event ended – lantian May 26 '20 at 09:09
  • 1
    call.playDtmfTone('1'); call.stopDtmfTone(); – lantian May 26 '20 at 09:12
  • try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mTrueCallerOngoingCall.getsCall().playDtmfTone((char) tone); mTrueCallerOngoingCall.getsCall().stopDtmfTone(); } else showToast("This device is not supported"); } catch (Exception e) { showToast("This device is not supported!"); } – Stevie May 26 '20 at 09:16
  • I did but nothing changed. – Stevie May 26 '20 at 09:16
  • Sorry.I don't know why. In my project it is working. the same function for us but it do's not work? maybe check the call obj? – lantian May 26 '20 at 09:37
  • It's working now. The problem here is playDtmfTone((char) tone). Maybe it wrong by convert like this. I have updated and fixed in the post :) many thanks ! – Stevie May 26 '20 at 09:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214660/discussion-between-thin-kopites-and-lantian). – Stevie May 26 '20 at 09:44