1
votes

I'm creating a Reminder application (it's like an alarm clock) you can set tasks in it.

The code for receiving the previously set alarm intent (with setExactAndAllowWhileIdle):

@Override
public void onReceive(Context context, Intent intent) {
    int alarmId = intent.getIntExtra(Constants.REMINDER_ALARM_ID, 0);

    Intent goingOffIntent = new Intent(context, GoingOffActivity.class);
    goingOffIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    goingOffIntent.putExtra(Constants.REMINDER_ALARM_ID, alarmId);

    context.startActivity(goingOffIntent);
}

This works even if the screen is off (and keyguard is on), GoingOffActivity appears with two options: snooze, dismiss.

GoingOffActivity's onCreate:

getWindow().setFlags(
            flags: WindowManager.LayoutParams.FLAG_FULLSCREEN |
                  WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                  WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                  WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                  WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            mask: WindowManager.LayoutParams.FLAG_FULLSCREEN |
                  WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                  WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                  WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                  WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

...

// Play ringtone
if (ringingEnabled) {
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);
    ringtone.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build());
    ringtone.play();
    //mediaPlayer = MediaPlayer.create(context, uri); //Settings.System.DEFAULT_RINGTONE_URI
    //mediaPlayer.setLooping(true);
    //mediaPlayer.start();
}

The problem is that the music (started with either Ringtone or MediaPlayer) plays when the phone is on (on home screen or in the app), but doesn't play when the phone wakes up from sleep. Only the sound is the problem everything else works just fine.

I did some research on the Internet, I guess this has something to do with Doze mode (maybe not, because I think when the activity starts the phone is not in Doze mode anymore), tried some solutions but nothing worked.

Api Level info:

  • App's Min SDK level: 23
  • Lenovo P2's level: 24 (test device)
  • Samsung A40's level: 28 (test device)

What should I do in another way? Thank you.

2
Have you tried firing notification with ringtone?Rahul Khurana
No, it has a dedicated activity for it, with dismiss and snooze possibilities. No need for a notification too.Mitulát báti

2 Answers

3
votes

I know what the problem was. I was stopping the MediaPlayer in onPause and it was called before the activity got visible (I don't know why).

2019-10-19 02:38:00.365 LOG:onReceive
2019-10-19 02:38:00.428 LOG:onCreate
2019-10-19 02:38:00.529 LOG:onResume
2019-10-19 02:38:00.542 LOG:onPause
2019-10-19 02:38:00.592 LOG:onStop
2019-10-19 02:38:00.716 LOG:onResume
0
votes

I think maybe you can solve your problem by implementing a WakeLock in your receiver, acquiring it before triggering the alarm, then release it after user stops alarm sound. Take a look at: https://stackoverflow.com/a/27287511/8513494, to see how implement a WakeLock that turn on the screen.