3
我正在構建一個鬧鐘應用程序,當手機靜音時,我想讓它播放鬧鐘,但保持靜音狀態。我現在擁有的鬧鐘應用程序(默認情況下是三星/安卓系統的一款)可讓手機保持靜音模式,但您仍然可以聽到鬧鐘。當手機靜音並保持手機靜音時播放鈴聲?
我試着設置它,所以我檢查音頻是否靜音或振動,設置聲音,然後重置音頻。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//get audiomanager and set the volume
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int oldVolume = 0;
int maxVolume = 0;
boolean silent = false;
SoundMode soundMode = null;
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT
|| audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
soundMode = soundMode.SILENT;
} else if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
soundMode = soundMode.VIBRATE;
}
oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
maxVolume = maxVolume/2;
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, AudioManager.FLAG_ALLOW_RINGER_MODES);
silent = true;
Log.i(TAG, "silent if statement " + soundMode);
}
// audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume/2, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtoneAlarm = RingtoneManager.getRingtone(getApplicationContext(), alarmTone);
ringtoneAlarm.play();
if (silent) {
if (soundMode == SoundMode.SILENT) {
Log.i(TAG, "silent mode");
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else if (soundMode == SoundMode.VIBRATE) {
Log.i(TAG, "vibrate mote");
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
return START_NOT_STICKY;
}
我已經得到了地方的電話知道什麼時候它是無聲或振動模式的一個點,但它不玩鈴聲,只是無論是振動或不會發出噪音。我該怎麼做才能播放鈴聲,不管怎麼樣,但保持音頻狀態的方式?