我的要求是在啓動撥出電話時打開揚聲器電話。我嘗試了下面的代碼,但它不起作用。實際上,在通話過程中,免提電話開啓,第二個電話就會來臨!每當打出電話時打開揚聲器電話
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();
}
}
}
謝謝。
我在這裏發現了一個類似的問題,也沒有回答:http://www.anddev.org/novice-tutorials-f8/how-to-programmatically-turn-on-loudspeaker-when-make-receiv- t49421.html。這是Android的錯誤嗎? – AllSolutions 2012-01-12 16:24:35
在這裏看到我的答案:http://stackoverflow.com/a/25346769/2826409 – 2014-08-17 06:24:16