简体   繁体   中英

Android set speakerphone on programmatically

I am trying to set speakerphone on in a call programmatically using audiomanager.setSpeakerphoneOn(true) in a service but it seems it is not working and I don't know why. Should I do something else?

Thanks

check if your manifest file has the permissions need to do this operation.

I think this is the permission you need MODIFY_AUDIO_SETTINGS

In android 4.1 and more when you make a call the phone turn off the speaker phone automatically. So What you need to do is to add the speakerphone on in the reciever that listens for the call being made when the state is "offHook" and even put a 0.5 second delayed to turn on the speaker like that:

final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    MainActivity.shouldTurnSpeakerOn = false;
    MainActivity.shouldTurnSpeakerOff = true;
    Log.d("incoming_call","speaker_on");                    
    }
}, 500);

Remember to add this to the Phone state listener. And to run off the speaker phone when the state is IDLE (needed for earlier versions).

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM