I know that Wakelocks are a thing. I use it in my code, however, I don't think it works. I use a TimePicker which the user can choose a time, and I later set a alarm for that time like this:
receiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent)
{
// Code here when time is hit...
}
};
_thisContext.registerReceiver(receiver, new IntentFilter("com.basse.bla.blabla"));
PendingIntent pintent = PendingIntent.getBroadcast(_thisContext, 0, new Intent("com.basse.bla.blabla, 0);
// Check if chosen time is the next day
Calendar now = Calendar.getInstance();
if (calendar.before(now)) {
calendar.add(Calendar.DAY_OF_MONTH,1);
}
// Set an alarm exact when the time is hit
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
PowerManager powerManager = (PowerManager) _thisContext.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "blabla:wakelog");
wakelock.aquire();
The important thing is that when OnReceive fires, I want to start a foreground service. I've read that AlarmManager aquires a wakelock? Let's say that the time is 3:24 PM. I choose with the TimePicker that I want the "alarm" to fire at 6:48 PM.
The problem is that if I leave my phone between that timeperiod, the phone goes to sleep/doze mode. I know that I can request the user to turn off battery optimizations, but many users find that difficult. How can I keep the alarm "running" in the background, so that it fires at the right time?