Alternative to the Alarmmanager

Asked

Viewed 114 times

2

I would like to know an alternative to scheduling a notification other than with Alarmmanager. The reason is that when the phone is turned off and then turned on, the scheduled task is removed.

2 answers

2


The "alternatives" are:

  • Jobscheduler

    Requires API Level 21 - See, in this reply, alternatives running at lower level API.

  • Workmanager

    New api included in the brand new Android Jetpack.
    Uses support libraries, allowing backward compatibility.

Note that these api’s do not replace the Alarmmanager, namely the accuracy of the time when the tasks are performed.

If you want to use Alarmmanager, you can solve the problem of removing the alarm after disconnecting and calling from the phone by following the indicated in this reply.

  • In case for me to schedule with Jobschedueler on a specific date, from what I saw and researched I would have to take the date and turn it into second right? or has a way to pass a triggerAtMillis?

  • Jobschedueler does not have triggerAtMillis.

1

I believe there is a misunderstanding in the understanding of the functioning of AlarmManager. You do not need to control the time that has passed, or the time that is missing. The tool uses the parameter long triggerAtMillis which is the time that will run the alarm. This time is the amount of milliseconds since 01-01-1970. No day 01-07-2018 às 12:00:00 at all times will have the value of 1530457200000 milliseconds. When the phone is restarted and send this value to the alarm will keep the same date. So if you want to keep the same alarm time even with your phone turned off or not, all you need to do is store the time you want to reschedule when he 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 right date is to use the site currentmillis.

  • It may be that yes, I tested this in the emulator, I think it is better to test in a real phone.

  • This is true but take note that the alarm has to be recorded again after the mobile reboot.

  • that same @ramaral, it is necessary to set again when the device starts to schedule the same time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.