Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn on speakerphone whenever an outgoing call is made

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.

like image 910
AllSolutions Avatar asked Dec 23 '11 14:12

AllSolutions


People also ask

Can I make my phone automatically speakerphone?

Tap "New Task", then enter a name (optional) and tap the checkmark icon when you're done. At the bottom of the next screen, tap the "+" icon to add an action to this task. From here, select "Audio", then "Speakerphone".

How do I turn on speakerphone during a call?

Try tapping the green bar at the very top of the screen. If when on a call you can only see the number dialer screen, Tap the "Hide" text at the bottom right corner of the screen to return to the phone options. Then Tap the Speaker icon to turn on the Speakerphone.

Why does my iPhone automatically turn speakerphone on?

Helpful answers The correct place to adjust this feature is located under Settings > Accessibility > Touch > Call Audio Routing. From here, make sure "Speaker" isn't selected and instead choose "Automatic". We hope this information helps out!


1 Answers

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.

like image 99
Mukesh Parmar Avatar answered Oct 26 '22 22:10

Mukesh Parmar