Android - Mobile alarm according to system time

0

Hello.

I've done a lot of research and I've learned to do some cool things with AlarmManager and the like. I wanted to make an alarm clock for my cell phone for tests, the problem is that all the codes and tutorials that I find is to wake up in a while, even if it is to wake up after 24 hours, I do not know if it is ideal. >

I did not want something that alarmed after a certain time, I would like to know how I make an alarm that it activates according to the system time, that is, if it is to be active at 23:55 then it is always at this time, it does not matter if the person disables the application or if it advances the system time.

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

How can I do this, can anyone help me? From now on I thank you: D

    
asked by anonymous 16.10.2015 / 22:35

2 answers

2

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

Both are the second and third parameters that tell when and how often the alarm is released. The second parameter indicates when and the third periodicity.

So if, for example, you want an alarm that goes off at 8 o'clock every day, it would look something like this:

//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);
    
16.10.2015 / 22:57
0

Look, I was able to set the alarm the way I needed it this 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 has several flaws. If I leave it the way it is, every day the exact 20:48 it will wake up. The problem is if the user uses the Stop button of the application where it corresponds to

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

When the button is active, the alarm stops working, that is, if it is 20:45 and it goes to the application and then it uses the Start again to continue the application, the alarm goes completely lost, it already stops to wake up at 8:48.

In addition, doing some tests here, it only runs once a day, if it is 10/10 and it has already woken up, even if I return the system time to 20:47, when it is 20:48 it no more wake up, I know this is due to the command of the AlarmManager.INTERVAL_DAY, but I would not like to use it, I would like it to just wake up when you give the time, simple: cell phone time = 20:48: wake up. Regardless of the day or how many times it is. Is there a way?  What happens if I put 0 in place of AlarmManager.INTERVAL_DAY? I tested it and only realized that even if you spend 1 day later, it stops working.

I'm a little lost rsrs

    
17.10.2015 / 03:54