您可以在接收器中使用以下内容:
/** The listener. */
private final PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("TAG", "IDLE");
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
} else if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("TAG", "OFFHOOK");
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("TAG", "RINGING====>" + incomingNumber);
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
switch (audiomanager.getRingerMode()) {
case AudioManager.RINGER_MODE_VIBRATE:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
case AudioManager.RINGER_MODE_SILENT:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
}
}
break;
}
};