如何在Android Lollipop中检测是否接听电话?

时间:2015-02-03 04:26:34

标签: android android-contacts

如果未接听电话意味着呼叫持续时间为0秒,则Android棒棒糖早先检测到呼出是否得到应答。但是在Android Lollipop中,呼叫持续时间在呼叫获得应答之前开始。在Android棒棒糖中是否有任何方法可以检测到是否接听了拨出电话?

2 个答案:

答案 0 :(得分:0)

您可以使用Telephony Manager覆盖onCallStateChanged侦听器。

    @Override
    public void onCallStateChanged(int state, String number) {
        switch (state) {
            case TelephonyManager.CALL_STATE_OFFHOOK:
                // call established.
                break;
        }

答案 1 :(得分:0)

我有一个可行的解决方案来检测何时接听拨出电话!解决方案有点hacky,但它起作用,至少在三星S6与Android 6.0.1。我也在使用Android 7.1.1的Nexus 5x上进行了测试,但它没有用。

我这样做的方法是使用音频管理器将音频模式更改为MODE_IN_COMMUNICATION,当我将拨出呼叫的状态更改为EXTRA_STATE_OFFHOOK时。将音频模式更改为MODE_IN_COMMUNICATION后,我启动一个监听音频模式的线程,直到系统在应答呼叫时将其更改回MODE_IN_CALL。

以下是代码段:

final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (outgoingCallAnswerWatchThread != null){
    outgoingCallAnswerWatchThread.interrupt();
    outgoingCallAnswerWatchThread = null;
}

outgoingCallAnswerWatchThread = new Thread(new Runnable() {
    @Override
    public void run() {
        // by changing the audio mode to MODE_IN_COMMUNICATION
        // we can then detect when the call is answered
        // because android changes the audio mode back to MODE_IN_CALL
        // when that happens
        audioManager.setMode(MODE_IN_COMMUNICATION);

        while(callState == RINGING && !Thread.interrupted()){
            try {
                Thread.sleep(CALL_WATCHER_SLEEP_TIME_MS);
            } catch (InterruptedException e) {
                Log.d(TAG, "Thread interrupted!");
                break;
            }


            int audioMode = audioManager.getMode();

            Log.d(TAG, "audioMode:" + audioMode);

            if (audioMode == MODE_IN_CALL){
                //the call is connected
                Log.i(TAG, "OUTGOING CALL answered!");
            }

        }

    }
});


outgoingCallAnswerWatchThread.start();