background task stops the moment the phone enters into the sleep state.
I would suggest you set alarm with alarmmanager and to use setExactAndAllowWhileIdle
for devices with 6.0 or greater. (checkout answer by @Jo Jo Roid here)
Please advice what architecture or API to use.
You can get long time = System.getCurrentTimeInMillis();
and then creating Calendar instance setting time to calender.setTimeInMillis(time + (5 * 60 * 1000));
AlarmManager am;
Intent intent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
if (Build.VERSION.SDK_INT >= 23) {
// Wakes up the device in Doze Mode
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time,
pending);
} else if (Build.VERSION.SDK_INT >= 19) {
// Wakes up the device in Idle Mode
am.setExact(AlarmManager.RTC_WAKEUP, time, pending);
} else {
// Old APIs
am.set(AlarmManager.RTC_WAKEUP, time, pending);
}
When your BroadcastReceiver receives Broadcast, set next another alarm by adding 5 mins to current time. Repeat this for 11 times when you receive broadcast and play Ding sound from AlarmStream. Simple!
You might be wondering "Why not use repeating alarm". Well to allow while in idle above M its the only workaround.
Also to show progress you can show notification with progressBar. (Because where else will you show progress if user leaves app or removes it from recents?
P.S. Don't forget to register your BroadcastReceiver and ask WAKE_LOCK permission in Manifest also.