Turn off AlarmManager when phone goes to sleep mode - android

I have AlarmManager set up to fire every minute. However, I only need it when phone is active. I want to turn off all updates when phone goes to sleep. I am not sure what the default behavior of AlarmManager and widgets in general is when phone goes to sleep. But I noticed that my battery goes down when in sleep mode. Hence, i believe its the widget thats bringing it down.
So, if AlarmManager and other broadcasts keep firing by default, how can I turn these updates off in sleep mode and then turn them back on when phone resumes active mode?

Use public void setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) with RTC as type. Only RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP will wake up devices.
E.g. in my code I use
am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + syncInterval, syncInterval, pendingIntent);

Related

What will alarm manager do if I change phone's time?

I set multiple alarms and in order to control them I change my phone's time. They will work normally ? What In other words, will alarms collapse if I change my phone's time?
Well it all depends on the type you have passed in the set method of alarm Manger.
if you have used ELAPSED_REALTIME or ELAPSED_REALTIME_WAKEUP, alarm will not trigger
ELAPSED_REALTIME
Added in API level 1
int ELAPSED_REALTIME
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
if you have user RTC or RTC_WAKEUP, alarm will be triggered according the device time
RTC
Added in API level 1
int RTC
Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
I don't know what you mean by collapse. But the alarm is only aware of the device's time and so if you do change your device time, it should work against that instead.

Android AlarmManager elapsedTime fires wrong

Ok, so I'm setting an alarm:
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, pendingIntent);
where
long triggerTime = SystemClock.elapsedRealtime() + mynterval;
I'm saving triggerTime for comparison in future.
And sometimes alarm is firing before triggerTime!
For example, I can see in logs:
scanTime: 702672466
SystemClock.elapsedRealtime: 702672132
What delta error is possible here - 1 second, 2-3 seconds or more?
And why is this happening?
This is happening due to Android Doze Mode.
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services.
Now, alarm is the case of CPU-intensive services. As you can see in the image the intensive jobs are done together in so-called maintenance windows.
Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.

How to prevent triggering alarm when screen is off?

I'm trying to trigger the alarm in a specific interval only when the device is awake, This is the code which I used to achieve this:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime(),
INTERVAL_ONE_MINUTE,
pi);
Considering documentations about AlarmManager.ELAPSED_REALTIME android is not supposed to trigger the alarm when device screen is off (If I mistake not)
Alarm time in SystemClock.elapsedRealtime() (time since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up
Contrary to my expectation alarm was triggered by system when the screen is off, Am I missing something here?
Why are you not using
protected void onPause();
to fire there
pi.cancel();
alarmManager.cancel(pi);
When they speak of the device going to sleep, that doesn't mean the screen is off.
See: When does android device go to sleep mode?
See: http://developer.android.com/training/scheduling/wakelock.html
When the device's screen isn't on, it still will be woken up to do things from time to time. To save battery, it'll sleep, but apps will inevitably wake it up to do things.
You could check to see if the screen is on: How can I tell if the screen is on in android?
The documentation you cite is just saying that the alarm won't fire when the device goes to sleep when ELAPSED_REALTIME is used.
Sounds like you want the alarm to fire when the application process is still alive. You might look at using ActivityLicecycleCallbacks:
http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html
This will let you schedule/cancel the alarm based on the process lifecycle as opposed to a single activity.
You might also look at
Application.onTrimMemory
to see when it gets called by the OS. You might find some use cancelling your alarm there.

What is the difference between RTC and RTC_WAKEUP of AlarmManager

Hello I was reading on android docs about these two constans of AlarmManager but didn't get exactly the difference between them.
RTC Alarm time in System.currentTimeMillis() (wall clock time in UTC).
RTC_WAKEUP Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.
Does not RTC wake up the device and fire the PendingIntent when device is in sleeping mode ?
Thanks in advance.
Does not RTC wake up the device and fire the PendingIntent when device is in sleeping mode ?
RTC and ELAPSED_REALTIME do not wake up the device out of sleep mode. If the device is in sleep mode at the time of the event, nothing immediately happens. You will be notified about missed events when the device wakes up for other reasons (e.g., user presses the power button).
RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP will wake up the device out of sleep mode. If your PendingIntent is a broadcast PendingIntent, Android will keep the device awake long enough for onReceive() to complete. If you have significant work, that you do not want to do in onReceive() (because onReceive() is called on the main application thread), you will need to arrange to keep the device awake long enough for some service of yours to complete the work, such as by using WakefulBroadcastReceiver.

Android: pause Widget updates using Alarmmanager

I just went through this tutorial:
update-widget-in-onreceive-method
(btw: would you propose any improvements to that code?)
At the end someone mentions:
I'm just wondering if there is a way to extend this further so that when the device is asleep (screen off), the updates stop. Then when the device wakes up, the updates resume.
So my question: is there a way of doing this? how?
or is the alarmmanager automatically stopped? - I don't think so.
You can specify whether the device will wake up when scheduling the AlarmManager.
Quote from the documentation:
ELAPSED_REALTIME
Alarm time in SystemClock.elapsedRealtime() (time
since boot, including sleep). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.
ELAPSED_REALTIME_WAKEUP
Alarm time in
SystemClock.elapsedRealtime() (time since boot, including sleep),
which will wake up the device when it goes off.
Same goes for AlarmManager.RTC and AlarmManager.RTC_WAKEUP
So you probably want one of the two AlarmManager.RTC or AlarmManager.ELAPSED_REALTIME. These continue while the device is awake and stop when the device is in standby. If this alarm is triggered while the device is asleep it will be delivered when the user turns the device back on though, exactly what you want. And no, the AlarmManagers scheduled alarms are not cancelled automatically in general.
The part mentioned here can be found in this part of the tutorial, specific this line:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20*1000, pendingIntent);

Categories

Resources