I am trying to turn speaker on while making a call. I have tried this code. Register new phone listener. But this doesn't work. It shows toast message, but speaker is still silent (only earpiece by default).
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(true);
break;
}
}
Of course I added permisions in my AndroidManifest.xml.
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
EDIT
This aslo doesn't work. It returns false (Toast message is false);
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
((Activity)mContext).setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
boolean check = audioManager.isSpeakerphoneOn();
Toast.makeText(mContext, "Speaker is on :" +String.valueOf(check),Toast.LENGTH_SHORT).show();
break;
I had the same problem because of custom rom. I have installed stock ROM and everything works great, here is my code
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
if (!audioManager.isSpeakerphoneOn())
audioManager.setSpeakerphoneOn(true);
audioManager.setMode(AudioManager.MODE_NORMAL);
Please try in your CALL_STATE_OFFHOOK
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
audioManager.setMode(AudioManager.MODE_NORMAL);
boolean check = am.isSpeakerphoneOn();//Toast it or Log it
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.