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

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.

Related

Is it because of Doze mode in android, I can never create an accurate alarm clock app using AlarmManager?

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

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.

About the difference between the alarms of type of AlarmManager.RTC_WAKEUP and AlarmManager.RTC

What is the difference between the alarms of type of RTC_WAKEUP and RTC?
From the documenatation of the AlarmManager, it seems that alarms of RTC will not wake up the device if the device current is asleep, and will not delivered until the next time the device wakes up.
But I found that the alarm of type of RTC will still wake up the device, so what's going on?
What I did is to change the type of RTC_WAKEUP to RTC in the AlamrManagerService.java, function setRepeating(), and then set an alarm of 3 minutes from now on, then check the device, you will find that it still could be waken up by the alarm.
THere are other instances reporting AlarmManager.RTC seems to wake up the device:
AlarmManager.RTC doing too many updates on wakeup
Android AlarmManager RTC doesn't pause while device is sleeping
Allowing the phone to sleep while using RTC alarm on Android
Each time, the root cause is the same:
Something keeps the device awake (even though the screen is black).
Don't forget that a charging device (connected to power) isn't sleeping.

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);

Android Alarm What is the difference between four types of Alarm that AlarmManager provides and when to use what?

I want to know the difference between RTC, RTC_WAKEUP, ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP.
I want to write an alarm application where I will set alarm and close my application and expect for alarm for the time set.
There will be multiple alarms. Right now I am writing for emulator but later will test on device. In emulator, once I set the alarm and close the emulator and restart it, then will it be cleared, as I find with RTC, RTC_WAKEUP and ELAPSED_REALTIME. I am confused. Should I used ELAPSED_REALTIME_WAKEUP? I have not seen any tutorial using ELAPSED_REALTIME_WAKEUP.
please explain.
Thanks.
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.
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.
RTC_WAKEUP
Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.
Types of Alarms :
ELAPSED_REALTIME – Fires the pending intent after the specified length of time since device boot. If the device is asleep, it fires when the device is next awake.
ELAPSED_REALTIME_WAKEUP – Fires the pending intent after the specified length of time since device boot. It wakes up the device if it is asleep.
RTC – Fires the pending intent at a specified time. If the device is asleep, it will not be delivered until the next time the device wakes up.
RTC_WAKEUP – Fires the pending intent at a specified time, waking up the device if asleep.
There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.
Source: https://developer.android.com/training/scheduling/alarms.html
From the site you can get the difference between the 4 constanst
Below is example of the setting alarm
Calendar mCalendar = Calendar.getInstance();
mCalendar.add(Calendar.SECOND, 20);
Intent intent_Timer = new Intent(TimerEvents.this, AlarmReceiver.class);
intent_Timer.putExtra("alarm_message", "Drax Rules!!!");
// In reality, you would want to have a static variable for the request
// code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
intent_Timer, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), sender);
Hope this will be helpful to you

Categories

Resources