androidandroid-mediaplayeralarmmanagerandroid-alarmsandroid-doze

MediaPlayer (or Ringtone) doesn't start playing when the screen is off (phone is locked)


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:

What should I do in another way? Thank you.


Solution

  • 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