I would like an alternative to schedule a notification other than AlarmManager. The reason is that when the phone is turned off and then turned on, the scheduled task is removed.
I would like an alternative to schedule a notification other than AlarmManager. The reason is that when the phone is turned off and then turned on, the scheduled task is removed.
The "alternatives" are:
Requires API Level 21 - See, in this response , alternatives that run under API Level lower.
New api included in the brand new Android Jetpack . Uses support libraries, allowing backward compatibility.
Please note that these api's are not a substitute for AlarmManager, particularly as regards the timeliness of the tasks.
If you want to use AlarmManager, you can solve the problem of removing the alarm after disconnecting and connecting from the mobile phone following the instructions in this answer .
I think there is a misunderstanding of how AlarmManager
works. You do not have to control the time that has passed, or the time that is missing. The tool uses the long triggerAtMillis
parameter which is the time that will execute the alarm. This time is the number of milliseconds since 01-01-1970. On day 01-07-2018 às 12:00:00
always will have the value of 1530457200000
milliseconds. When the phone restarts and sends this value to the alarm will keep the same date. So if you want to keep the same alarm time even with the cellphone off or not, all you have to do is store the time you want to schedule again when it calls, for example 01-05-2018 12:01:05
:
int dia, mes, ano, hora, minuto, segundo;
dia = 1; mes = 5; ano = 2018; hora = 12; minuto = 1; segundo = 5;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, dia);
calendar.set(Calendar.MONTH, mes);
calendar.set(Calendar.YEAR, ano);
calendar.set(Calendar.HOUR_OF_DAY, hora);
calendar.set(Calendar.MINUTE, minuto);
calendar.set(Calendar.SECOND, segundo);
long milisegundos = calendar.getTimeInMillis();
milisegundos
will always have the value of 1525186800000
. A good tool to test if you pulled the correct date is to use currentmillis .