If I set a repeating alarm on my application's first run. Then what are the chances that affects the disabling of alarm. I mean if I do not interfere with the alarm then does there is a chance that the android OS will disable it?
Alarm will not be killed by the OS like a normal service.
In case of repeating alarms, behavior differs from api 21 (or api 19, dont remember). After API 21, repeating alarms are not 'setexact()' i.e. OS will optimize them and alarms may not go off at the exact time specified.
In case of reboot, OS will not automatically reload the alarms for you. You have to handle the logic in 'RECEIVE_BOOT_COMPLETED'
Related
I have to update Data in my App every 24 hours at 2 am.
Currently, I have an Alarm via the AlarmManager which sends an alarm every 24 hours with the setRepeating method.
In the past I have experienced some unreliabilities with the timing of the alarm, so I was experimenting with an intent-filter and Intent.ACTION_TIME_TICK.
My Question:
What is the difference between setting a repeated alarm every 24 hours and using an intent-filter which gets its information from the system?
You should absolutely not do anything with ACTION_TIME_TICK. Nor would it be reliable if you tried- it would only work while your app is running, and if you're backgrounded you will be killed for resources. The correct answer here is actually JobScheduler or WorkManager, depending on the nature of what you're doing. Most likely WorkManager. However if you were worried about the reliability of timing of an alarm, you probably are thinking about your problem wrong. Unless you have a VERY niche use case, a bit of inaccuracy in downloading a nightly update is generally ok. In fact it may even be welcome, to spread the load out on the server. Your inaccuracy of dl time is likely due to Doze, which is a mechanic your use case should likely account for (Doze is a power saving mode where it reduces the frequency of timed events running when the screen is off).
In my app I used Alarm manager for time scheduling. For that I used Alarm Manager API .
I am using setInExactRepeating() method for repeat my alarm but it's not triggering my alarm when my device is in idle condition. Also sometimes it delays to triggering alarm.
For my app its important to trigger the alarm at the exact time in repeating mode also in idle condition.
Please help how can i resolve this issue, I also learned about Job scheduler and Work manager but these have not a feature like alarm manager to set alarm at exact or repeat.
Thank You
You probably can't do any with repeat because on higher android there is a limitation for repeating (i think 15m) for better performance and battery saving and Alarm manager on higher android isn't the best.
Even if you manage to do so and make your app it will probably be killed by performance controller applications or even android OS itself. and I don't suggest investing your time in it because of this.
There were few tricks to trick the android OS and get your app running but I think it will be really messy to code that. the best thing to work is the same WorkManager as you say
I am using AlarmManager in our app to set alarms that are set at specific date and time. Now recently few users of our app are complaining that these alarms are not popping up. Also, in Android O guidelines, it is mentioned that app should not run any background service and instead should switch to Firebase JobDispatcher. I have following 2 questions
In our app, we do not do any background task except to show notification to user at the specified time and date. Even in this case, should we switch to Firebase Jobdispatcher?
In case we do need to switch to JobDispatcher, how can the Job be set to run at exactly specific date and time?
Because you're not doing any background tasks like network requests or long running CPU operations, I think you should continue using Alarm Manager.
Now recently few users of our app are complaining that these alarms are not popping up.
This is because when doze mode triggers, it is not guaranteed that your alarms will be triggered(see this).
What you can do is use setAndAllowWhileIdle() or setExactAndAllowWhileIdle() methods from AlarmManager class as per your requirement for API level >= 23.
According to the documentation these are appropriate for notification purposes.
setAndAllowWhileIdle() documentation:
Like set(int, long, PendingIntent), but this alarm will be allowed to
execute even when the system is in low-power idle modes. This type of
alarm must only be used for situations where it is actually required
that the alarm go off while in idle -- a reasonable example would be
for a calendar notification that should make a sound so the user is
aware of it. When the alarm is dispatched, the app will also be added
to the system's temporary whitelist for approximately 10 seconds to
allow that application to acquire further wake locks in which to
complete its work.
I am using AlarmManager, trying to create an Alarm app for android.
I noticed that setRepeating was not working when the phone sleeps.
So, I tried setExactAndAllowWhileIdle.
But, I read this:
Unlike other alarms, the system is free to reschedule this type of alarm to happen out of order with any other alarms, even those from the same app. This will clearly happen when the device is idle (since this alarm can go off while idle, when any other alarms from the app will be held until later), but may also happen even when not idle. Note that the OS will allow itself more flexibility for scheduling these alarms than regular exact alarms, since the application has opted into this behavior. When the device is idle it may take even more liberties with scheduling in order to optimize for battery life.
I need accurate timings like an alarm clock. A user sets it for 6:00 am then ringing at 6:01 or 6:02 would be wierd!
Not ringing at all because the phone is idle is catastrophic!
What can I do now?
Do not use repeating alarms for this purpose. They are not accurate/reliable enough. Schedule one alarm using set() or setExact() (depending on your target API level). When that alarm goes off, set the next one.
NOTE: Make sure that you use an alarm type that will wake the phone:
RTC_WAKEUP or
ELAPSED_REALTIME_WAKEUP
I wanted to make a widget that updates every sec(more battery consumption) or min(less battery consumption).
I followed as in this thread
, but runs only in every 30mins.
I configured that, once onUpdate is run, it updates in 1mins and
onReceived is run in every 30mins.
Can anyone tell me what I am doing wrong?
From my experience AlarmManager doesn't work well (or at all) with intervals lower than 1 minute. Besides:
Note: The Alarm Manager is intended for cases where you want to have
your application code run at a specific time, even if your application
is not currently running. For normal timing operations (ticks,
timeouts, etc) it is easier and much more efficient to use Handler.
Moreover:
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS
will shift alarms in order to minimize wakeups and battery use. There
are new APIs to support applications which need strict delivery
guarantees; see setWindow(int, long, long, PendingIntent) and
setExact(int, long, PendingIntent). Applications whose
targetSdkVersion is earlier than API 19 will continue to see the
previous behavior in which all alarms are delivered exactly when
requested.
You are doing nothing wrong, the widget service indeed only updates every 30 minutes(minimum).
To make it update faster you need to use AlarmManager, or your own application that will call the service. Unlike soulreaver answer, the alarm manager does work with times less then 1 minute.