Android - Alarm for mobile according to system time

Asked

Viewed 2,892 times

0

Hello.

I’ve done a lot of research and I’ve learned to do some nice things with Alarmmanager and stuff like that. I wanted to make an alarm for my mobile for testing, the problem is that all the codes and tutorials I find is to wake up in a while, even if it is to wake up after 24 hrs I do not know if it is ideal.

I didn’t want something to alarm him after a while, I’d like to know how I make an alarm that he triggers according to the system time, that is, if he’s supposed to be active at 11:55 then it’s always at this time, does not matter if the person disable the application or if it advances the system time.

Like every alarm I’ve encountered, there’s a broadcast that’s active after a while. I thought of making that broadcast have a new trhead with a while checking every minute the system time to see if it’s according to what the person put in. Would that work? Because if it works I think it should consume a lot of battery, imagine a While running for 24 hrs ?

How can I do that, can anyone help me ? Already I thank :D

2 answers

2


To make a repeat alarm you must use one of the following Alarmmanager methods: setInexactRepeating() or setRepeating().

In both are the second and third parameter that inform when and with what frequency the alarm is launched.
The second parameter indicates when and the third the periodicity.

So if, for example, you want an alarm that goes off at 8 a.m.:

//Cria um Calendar "setado" para as 8 horas
//para ser passado no segundo parâmetro.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);

//Passa-se a constante AlarmManager.INTERVAL_DAY ao terceiro parâmetro
//para indicar que a periocidade é diária  
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);
  • I got an idea, thank you very much :)

0

Look, I managed to set the alarm the way I needed it that way, using the following information::

Calendar cal = Calendar.getInstance();

    cal.set(Calendar.HOUR_OF_DAY, 20);
    cal.set(Calendar.MINUTE, 48);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.AM_PM, Calendar.PM);

    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);

It works well if the user does not touch anything else. The problem is that it is with several flaws. If I leave it the way it is, every day at exactly 8:48 he will awaken. The problem is if the user uses the Stop button in which the application corresponds to

AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.cancel(pi);

When the button is active, the alarm clock stops working, that is, if it is 20:45 and he stops the application and then he uses Start again to continue the application, the alarm clock is all lost, it already stops waking up at 20:48.

Also, doing some tests here, it only runs once a day if it is dua 17/10 and it has already awakened, even if I turn the system time to 20:47, when it is 20:48 it does not awaken anymore, I know this is due to the command of Alarmmanager.INTERVAL_DAY, but I would not like to use this, I would like you to just wake up when this time, simple: cell phone time = 20:48: awake. Regardless of the day or how many times. Does it have a ? What happens if I put 0 in place of Alarmmanager.INTERVAL_DAY ? I tested and only realized that even if you pass 1 day later, it stops working.

I’m a little lost rsrs

  • Do not use the answer box to ask a question. If you have a new question use the button in the upper right corner of the page or follow this link.

Browser other questions tagged

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