Turn on speakerphone whenever an outgoing call is made
Asked Answered
G

2

6

My requirement is to turn on speakerphone whenever an outgoing call is initiated. I tried the following code, but it is not working. In fact, speakerphone turns on when in the middle of a call, a second call comes !

package in.co.allsolutions;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
//import android.view.View;
import android.widget.Toast;
import android.media.AudioManager;

public class MyTelephonyBroadcastReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {

            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(true);
            Bundle extras = intent.getExtras();            
            if (extras != null) {

                  String state = extras.getString(TelephonyManager.EXTRA_STATE);
                  Log.i("AS", "Message Received. State = " + state + ", Mode = " + audioManager.getMode());
                  //audioManager.setMode(AudioManager.MODE_NORMAL);
                  //audioManager.setSpeakerphoneOn(true);
//                  if (state.equals("OFFHOOK"))
//                  {                  
                  //audioManager.setMode(AudioManager.MODE_CURRENT);
                  //audioManager.setSpeakerphoneOn(true);
                  //audioManager.setMode(AudioManager.MODE_IN_CALL);
                  //audioManager.setSpeakerphoneOn(true);
                  //audioManager.setMode(AudioManager.MODE_RINGTONE);
                  //audioManager.setSpeakerphoneOn(true);
                  if (audioManager.isSpeakerphoneOn()) {
                        Log.i("AS", "Speaker on - SUCCESS.");
                  } else {
                        Log.i("AS", "Speaker could not be turned on.");
                  }
//                  }
            } else {
                  Toast.makeText(context, "Message Received without any state", Toast.LENGTH_LONG).show();
            }
      }
}

Thanks.

Gadroon answered 23/12, 2011 at 14:53 Comment(2)
I found a similar question here, which too was not answered: anddev.org/novice-tutorials-f8/…. Is it a bug in Android?Gadroon
See my answer here : https://mcmap.net/q/775084/-android-set-speakerphone-on-programmaticallySupernormal
W
10

You can set it through programmatically as below :

AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE); 
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

But, keep in mind that don't forgot to set speaker off when stop the call:

audioManager.setSpeakerphoneOn(false);

And, Set permission in manifest:

  <uses-permission android:name="android.permission.RECORD_AUDIO" />
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

This code is working fine for me.hope it will be helpful for you.

Worth answered 24/1, 2014 at 9:4 Comment(1)
Thanks. In Kitkat 4.4.2 the RECORD_AUDIO permission is not need, i can't say for other android versions..Chook
G
5

A similar question was asked and answered here.

I think the answer may be in your project's AndroidManifest.xml. Try adding:

uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"

to your manifest, which will allow your app to modify the device's audio settings.
You will also need to change your audioManager mode to MODE_IN_CALL:

audioManager.setMode(AudioManager.MODE_IN_CALL)
Guileless answered 11/5, 2012 at 11:59 Comment(1)
That doesn't work any more. At least on a pixel 3, android 11.Tenne

© 2022 - 2024 — McMap. All rights reserved.