How to Turn Speaker On/Off Programmatically in Android 4.0

How do I enable the Android speaker during a call, from code?

I solved it. I created a thread that constantly runs the above three lines of code in a "While (true)". Works great.

How to turn on speaker programmatically in android

audiomanager.setSpeakerphoneOn(true)

Be sure to include the MODIFY_AUDIO_SETTINGS permission in your manifest.

How to turn on speaker for incoming call programmatically in Android L?

Finally, I got the solution. I put above code run inside a thread. It worked well. This is my code. Hope it can help someone

            Thread thread = new Thread() {
@Override
public void run() {
try {
while(true) {
sleep(1000);
audioManager.setMode(AudioManager.MODE_IN_CALL);
if (!audioManager.isSpeakerphoneOn())
audioManager.setSpeakerphoneOn(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};

thread.start();

Android - How to turn on speakerphone and max volume for phone call

a PhoneStateListener as its name suggests is just a listener, it can't perform actions on the current call, that's the responsibility of the default Phone app.

The good news is that you can have the user set your app as the default Phone app, but it forces you to implement certain activities and services which might be out of scope for your app.

You'll need to implement your own dialpad activity for making calls, your own InCallService for handling active calls, and your own in-call UI for allowing the user to mute, put on speaker, connect to bluetooth, put a call on call-waiting, send dialtones while in a call, etc.

You can check out this series of medium posts on how to make your app the default Phone app.

Once you've made your app the default phone app, and your app controls the current app, you can use setAudioRoute to turn on speaker phone.

EDIT:

You can try using setSpeakerphoneOn in AudioManager (isX is always used to check a value, setX is used to modify a value), however the docs say:

This method should only be used by applications that replace the
platform-wide management of audio settings or the main telephony
application.

I'm not sure if Android enforces that by preventing apps from calling this method, and if so, on which Android platform, but you can try it out.

There is also a way to use java reflection to access hidden API, see here, but using reflection will almost definitely break on recent version of Android, and might block your app from getting into Google Play. Even still, might worth playing around with.

Android set speakerphone on programmatically

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

I think this is the permission you need MODIFY_AUDIO_SETTINGS



Related Topics



Leave a reply



Submit