以编程方式通话后,如何关闭扬声器电话,android?

时间:2019-07-01 04:50:37

标签: android android-intent

 Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        callIntent.setData(Uri.parse("tel:" + sharedPreferencesUtils.getStringFromSharedPreferences(KEY_SP_MOBILE_NUMBER, context.getApplicationContext())));
        context.getApplicationContext().startActivity(callIntent);

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while(true) {
                        sleep(300);//1000
                        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                        audioManager.setMode(AudioManager.MODE_IN_CALL);
                        if (!audioManager.isSpeakerphoneOn()) {
                            audioManager.setSpeakerphoneOn(true);
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

2 个答案:

答案 0 :(得分:1)

我将底层代码放在线程内运行。运行良好。这是我的代码。希望它可以帮助某人

        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); //or false
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

请确保在清单中包括MODIFY_AUDIO_SETTINGS权限。

答案 1 :(得分:0)

尝试这个

audiomanager.setSpeakerphoneOn(false)

为此,您应该在清单中添加MODIFY_AUDIO_SETTINGS权限。