2

AudioManager 在 onCallStateChanged 中不可靠。在通话期间,我需要它打开免提电话并将音量设置为最大。它有时会打开免提电话(通常在第二次或以后的通话中)并且很少调高音量。我的 PhoneCallListener 类在我的 MainActivity 类中。

private class PhoneCallListener extends PhoneStateListener
{
    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        AudioManager aM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        aM.setMode(AudioManager.MODE_IN_CALL);
        aM.setSpeakerphoneOn(true);

        if(TelephonyManager.CALL_STATE_RINGING == state)
        {
            //phone ringing
            aM.setSpeakerphoneOn(true);
            aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        }

        if(TelephonyManager.CALL_STATE_OFFHOOK == state)
        {
            //phone active
            aM.setSpeakerphoneOn(true);
            aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
            isPhoneCalling = true;
        }

        if(TelephonyManager.CALL_STATE_IDLE == state)
        {
            aM.setSpeakerphoneOn(false);

            if(isPhoneCalling)
            {               
                Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                isPhoneCalling = false;
            }
        }
    }
}

在 CALL_STATE_OFFHOOK 中,我不得不关闭 AudioManager.FLAG_SHOW_UI,因为它会不断显示音量 UI。此外,设置 aM.setStreamVolume(AudioManager.STREAM_MUSIC, aM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); 由于某种原因使应用程序崩溃。

关于如何让 AudioManager 每次都能正常工作,以便在通话期间打开免提和最大音量的任何建议?


编辑:即使在调用 onCallStateChanged 方法后将扬声器设置为 true,它仍然不能可靠地打开扬声器。音量也不可靠,似乎无法在不崩溃的情况下将其设置为最大。

4

1 回答 1

0

下面是执行此操作的代码。我已经在运行棒棒糖的手机中进行了测试。将您的 PhoneStateListener 编写为:

  private class myPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {

            case TelephonyManager.CALL_STATE_OFFHOOK: //Call is established
                Log.d("s@urav", "Call is Offhook now!");
                try {
                    Thread.sleep(500); //We never know when the call is actually OffHook
                } catch (InterruptedException e) {
                Log.d("s@urav","Exception is:"+e);
                }
                audioManager.setSpeakerphoneOn(true);
                break;

            case TelephonyManager.CALL_STATE_IDLE: //Call is finished
                    //Maintain a flag and do this only if speakerphone has been set on OFFHOOK
                    /*audioManager.setMode(AudioManager.MODE_NORMAL);
                    audioManager.setSpeakerphoneOn(false);*/
                break;
        }
    }
}

要提高通话音量,您必须提高 STREAM_VOICE_CALL 的音量。此代码+增加呼叫流的量将满足您的要求。

于 2016-06-01T13:05:43.963 回答