我正在使用 PhoneStateListener,因此当用户接听电话或开始通话时,扬声器将立即开启。
PhoneStateListener phoneStateListener = new PhoneStateListener()
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (state == TelephonyManager.CALL_STATE_RINGING)
{
} else if(state == TelephonyManager.CALL_STATE_IDLE)
{
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
{
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(true);
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null)
{
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
这是我使用的代码。在我安装果冻豆之前它工作得很好,现在我注意到它只在用户接到电话时才打开扬声器,但如果他开始打电话,它什么也不做