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

Deadly 提交于 2019-12-03 09:45:39

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!