I am writing an alarm (kinda) app, which registers a broadcast intent with AlarmManager with the RTC_WAKEUP flag, to go off at a specified time. Works...
When the broadcast is received, the receiver starts an activity that plays a ringtone with MediaPlayer using the STREAM_ALARM stream, and does some other stuff. All that works, but if the broadcast is received when the screen is off, the activity is starts up correctly but the ringtone does not play. Works fine and plays ringtone if received when the screen is on and unlocked.
Are there any additional flags to set? Here's the code of the activity started from the Receiver.
[HIGH]
public void onCreate(Bundle bundle) {
super.onCreate(bundle)
...
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ringtonePlayer = new MediaPlayer();
...
playRingtone();
}
private void playRingtone() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
ringtonePlayer.setDataSource(getRingTone(), RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE).toString()));
ringtonePlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
ringtonePlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
ringtonePlayer.setLooping(true);
ringtonePlayer.prepare();
ringtonePlayer.start();
Log.i(LOG_TAG, "Ringtone started");
}
[/HIGH]
When the broadcast is received, the receiver starts an activity that plays a ringtone with MediaPlayer using the STREAM_ALARM stream, and does some other stuff. All that works, but if the broadcast is received when the screen is off, the activity is starts up correctly but the ringtone does not play. Works fine and plays ringtone if received when the screen is on and unlocked.
Are there any additional flags to set? Here's the code of the activity started from the Receiver.
[HIGH]
public void onCreate(Bundle bundle) {
super.onCreate(bundle)
...
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ringtonePlayer = new MediaPlayer();
...
playRingtone();
}
private void playRingtone() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
ringtonePlayer.setDataSource(getRingTone(), RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE).toString()));
ringtonePlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
ringtonePlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
ringtonePlayer.setLooping(true);
ringtonePlayer.prepare();
ringtonePlayer.start();
Log.i(LOG_TAG, "Ringtone started");
}
[/HIGH]