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.