I am building an alarm clock app, and when the phone is silent, i want it to play the alarm but keep the phone mode on silent. The alarm app I have right now (defualt samsung / android stock one) keeps the phone on silent mode but you still hear the alarm.
I tried setting it so I check if the audio is silent or vibrate, set the sound and then reset the audio.
@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;
}
I've gotten to a point where the phone knows when it's silent or in vibrate mode, but it doesn't play the ringtone, it just either vibrates or doesn't make a noise. How do I make it so it plays the ringtone no matter what, but keeps the audio state the way it was?