我看过here。没有找到答案。
public class CallReceiver extends BroadcastReceiver {
private TelephonyManager mTelephony;
private IncomingPhoneStateListener mIncomingPhoneListener;
private String number;
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
try {
mContext = context;
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mIncomingPhoneListener = new IncomingPhoneStateListener();
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("CallReceiver", "onReceive error:" + e.getMessage());
}
}
public class IncomingPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// ******* This code should unmute and change volume to MAX
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), AudioManager.FLAG_PLAY_SOUND);
// ******* end
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_NONE); // unregister
}
}
}
当设备处于静音模式或振动模式时,上面的代码取消静音并将音量更改为最大值。 此代码适用于Nexus设备(例如Galaxy Nexus) 但是,它不适用于三星设备(S3,S4,注2)[Android版本4.3]。
有什么建议吗?
答案 0 :(得分:1)
由于我找不到答案,所以在我的服务中实际播放铃声,我想出了解决方法。