Do you need to remove an alarm before setting new one - android

I have an Android alarmManager being set and a broadcastReceiver to pick it up, whenever the app triggers onResume, I am forcing a new alarm to be set.
So I'm wondering for best practices and considering good resource management, should I be removing the first alarm (if it hasn't triggered) before I reset a new alarm ?
Potentially if an alarm doesn't trigger, each time the user re-opens the app, is it creating more resource usage even if the alarm is for the same time ?

Yes you should cancel it when it is appropriate.
I suspect you will have a lot of wasted cycles otherwise. For Example,
App opens at Time T.
Schedule an alarm for T'.
Next the user opens the app again at some time, X seconds prior to T'.
You will then schedule an alarm presumable at another time T''.
However you will get an alarm firing in X secs anyways. If you ignore it, you are simply wasting battery if the phone had to wake in order to deliver the alarm.

If you create the same PendingIntent for your alarm, then the previous one will automatically be canceled when you set it again.
See AlarmManager.set(), PendingIntent.

Related

how to sense that the scheduled time by AlarmManager was passed in rebooting

for example,An AlarmManager to count numbers at 7AM using Calendar was set.
But user turns off the android at 6am and turns on 8am.
So program does'nt count.
I dont think i can avoid this situation if i reset AlarmManager on receiver of BOOT COMPLETED.
Please tell me the way to count exact number in this situation.
You'd have to do a bit of work. Save the time of your next alarm in permanent storage (file, shared preference, or database). Whenever an alarm occurs, update this value. Set a BOOT_COMPLETED listener. When the boot completed listener launches, have it get the current time and check if its later than the time of the next alarm you stored. If so, you missed it. If not, you're ok.
Now if you have to worry about missing multiple alarms, it gets more complicated, but the idea is the same.

Everyday notifications at certain time

I would like to achieve this:
After first turning on the application, user receives notifications, every day at 2pm, if certain condition is true. If condition is false, we are not showing a notification this day. The condition is checked at 2pm, it downloads some data from the Internet.
So far I used AlarmManager and its method setRepeating() with 24h interval. AlarmManager fires up a Service. In this Service I'm downloading the data, checking condition and if it's true - showing Notification. Since downloading can last more than 5 seconds, I've declared android:process=":background" for this Service, to run it in separate process and not block my UI.
This approach has two drawbacks:
1: If user opens application let's say at 4pm (and the condition is true), he will receive the notification immediately. From setRepeating() documentation:
If the time occurs in the past, the alarm will be triggered
immediately, with an alarm count depending on how far in the past the
trigger time is relative to the repeat interval.
I would like that user will not receive a notification this day, only the next day and so on.
2: I'm worried that my notifications will not show after user switch the phone off. From AlarmManager documentation:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
I don't know if it's possible to make it work all the time.
If you have any ideas how to make it better, you're welcome.
1: I'm not quite sure if i understood your question, but I think all you have to do is if it already is past 2pm add a day to 2pm:
GregorianCalendar twopm = new GregorianCalendar();
twopm.set(GregorianCalendar.HOUR_OF_DAY, 14);
twopm.set(GregorianCalendar.MINUTE, 0);
twopm.set(GregorianCalendar.SECOND, 0);
twopm.set(GregorianCalendar.MILLISECOND, 0);
if(twopm.before(new GregorianCalendar())){
twopm.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
alarmManager.setRepeating(type, twopm.getTimeInMillis(), 1000*60*60*24, intent);
2: You could register a BroadcastReceiver for booting and start your alarm again there. Take a look at this: Android BroadcastReceiver on startup - keep running when Activity is in Background

Creating alarm with AlarmManager for a specific period of time

Is there any way to use AlarmManager to activate an alarm for a specific period of time? I have start-time and end-time values stored in the database. I want to start an alarm at start-time that will make the device silent and alarm should end at end-time when the device volume will be normal again.
One way is set alarm at start-time & then set another alarm at end-time. But the problem is the time period may overlap that will need additional logic to be implemented if I go with 2 different alarms(one at start-time, another at end-time). Is there any procedure in Android to cope with this situation? Or implementing logic is the only way to overcome this issue?
Why not just in your Intent for the PendingIntent pass an extra like "endTime" long type for the time you want to end the alarm. (im assuming its repetitive) then in your broadcast receiver get that extra, compare to System.currentTimeMillis() and if it is less then current time cancel the alarm and exit?

about Timing execution

i have a task: there are some data,but i want to delete it,so provides the user with options as 3 days ,5days ...to delete it. i want to use alarmmanager,but when the user close the device or modify the system time,the Timer will no longer accurately,how to avoid it .thank you
If you use AlarmManager with an RTC alarm, the alarm will adjust for changes to the system time. The possible exception would be if the time change skips over the time of the alarm -- I have not tried that.
For reboots, you will need to reschedule your alarms. This is typically accomplished via a BOOT_COMPLETED BroadcastReceiver.

How will my app receive an intent broadcasted by AlarmManager if the OS has closed my due to memory crunch

I am writing an alarm app, and would like to do some specific work when the alalrm is triggered. For this I use the AlarmManager.set() method and the pending intent broadcasts the intent. So far so good. But what if the OS decides to close my app which was in background due to memory crunch. Could someone help me with this.
Also if I want repeated alarms then I can use the AlarmManager.setRepeating(). Does this take into consideration the day light saving adjustment ?
The Alarm will start your application even if this isn't currently running. In order for this to happen you need to register a broadcast receiver either with the <receiver> tag in the manifest file or with the registerReceiver method. Read the documentation for details.
As for your second question, although I haven't tried it, with setRepeating you configure the interval between subsequent alarms. If for example you have your first alarm at 13:00 and use an INTERVAL_DAY interval, the alarm will fire every day at 13:00. If daylight savings take affect and 13:00 becomes 12:00, then the alarm will fire daily at 12:00. However, I suppose you need to try this out.

Categories

Resources