简体   繁体   中英

How to turn off speaker phone after call programmatically , Android?

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

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

        thread.start();

Be sure to include the MODIFY_AUDIO_SETTINGS permission in your manifest.

Try this one

audiomanager.setSpeakerphoneOn(false)

For it you should add the MODIFY_AUDIO_SETTINGS permission in manifest.

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