如何在通话期间打开扬声器

时间:2014-12-20 09:20:33

标签: java android audio speakerphone

我打算在打电话时打开扬声器。我试过这段代码。注册新的电话听众。但这并不奏效。它显示吐司信息,但扬声器仍然是静音(默认只有听筒)。

@Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
                AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setSpeakerphoneOn(true);
                break;
        }
    }

当然我在AndroidManifest.xml中添加了permisions。

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

编辑

这也不起作用。 它返回false(Toast消息为false);

case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
                try {

                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                     Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
                audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
                ((Activity)mContext).setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
                audioManager.setMode(AudioManager.MODE_IN_CALL);
                audioManager.setSpeakerphoneOn(true);
                boolean check = audioManager.isSpeakerphoneOn();
                Toast.makeText(mContext, "Speaker is on :" +String.valueOf(check),Toast.LENGTH_SHORT).show();
                break;

2 个答案:

答案 0 :(得分:5)

由于自定义ROM,我遇到了同样的问题。我已经安装了库存ROM,一切都很好,这是我的代码

audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setMode(AudioManager.MODE_IN_CALL);
                if (!audioManager.isSpeakerphoneOn())
                    audioManager.setSpeakerphoneOn(true);
                audioManager.setMode(AudioManager.MODE_NORMAL);

答案 1 :(得分:0)

请尝试使用CALL_STATE_OFFHOOK

 setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); 
 audioManager.setMode(AudioManager.MODE_IN_CALL);                
 audioManager.setSpeakerphoneOn(true); 
 audioManager.setMode(AudioManager.MODE_NORMAL);                
 boolean check = am.isSpeakerphoneOn();//Toast it or Log it