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

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();


I came across this solution. hope it helps you.

final static int FOR_MEDIA = 1;final static int FORCE_NONE = 0;final static int FORCE_SPEAKER = 1;Class audioSystemClass = Class.forName("android.media.AudioSystem");Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);setForceUse.invoke(null, FOR_MEDIA, FORCE_SPEAKER);