Android alarm is 12 hours late - android

I have set an alarm (in an alarm clock app we are developing) for 8:58 (in 24h format).
The alarm did not fire - alarm icon is still in status bar - and when I write adb shell dumpsys alarm I can see this:
Batch{6b33bdb num=1 start=99803288 end=99803288 flgs=0x3}:
RTC_WAKEUP #0: Alarm{3e3da2e type 0 when 1484078280736 my.app}
tag=*walarm*:my.app/md5f98255d9820be5cc9672d0645bf1ca12.RingBroadcastReceiver
type=0 whenElapsed=+11h1m53s125ms when=2017-01-10 20:58:00
window=0 repeatInterval=0 count=0 flags=0x3
Alarm clock:
triggerTime=2017-01-10 08:58:00
showIntent=PendingIntent{21219cf: PendingIntentRecord{aac3f5c my.app startActivity}}
operation=PendingIntent{ecde88f: PendingIntentRecord{821c71c my.app broadcastIntent}}
Next wake from idle: Alarm{3e3da2e type 0 when 1484078280736 my.app}
tag=*walarm*:my.app/md5f98255d9820be5cc9672d0645bf1ca12.RingBroadcastReceiver
type=0 whenElapsed=+11h1m53s125ms when=2017-01-10 20:58:00
window=0 repeatInterval=0 count=0 flags=0x3
Alarm clock:
triggerTime=2017-01-10 08:58:00
showIntent=PendingIntent{21219cf: PendingIntentRecord{aac3f5c my.app startActivity}}
operation=PendingIntent{ecde88f: PendingIntentRecord{821c71c my.app broadcastIntent}}
THE QUESTION IS: How is it possible that when is 12 hours after triggerTime?
It seems like 12/24 hour format issue, but my device and also my app and its time picker are all set to 24 hour format. This issue happened just once. When I set a new alarm, it works as expected.
Any ideas?
UPDATE:
We are using this code to set the alarm (sorry for Xamarin syntax) and I have tested that the alarmEntity is returning correct time. The problem must be in this code or in the device/android/system stuff. From the triggerTime=2017-01-10 08:58:00 it seems to me that the code is OK and there is some issue in the system or alarm manager.
DateTime closestTrigger = alarmEntity.GetTime();
Calendar calendar = Calendar.Instance;
calendar.Set(CalendarField.Year, closestTrigger.Year);
calendar.Set(CalendarField.Month, closestTrigger.Month - 1);
calendar.Set(CalendarField.DayOfMonth, closestTrigger.Day);
calendar.Set(CalendarField.HourOfDay, closestTrigger.Hour);
calendar.Set(CalendarField.Minute, closestTrigger.Minute);
calendar.Set(CalendarField.Second, closestTrigger.Second);
var alarmInfo = new AlarmManager.AlarmClockInfo(calendar.TimeInMillis, mainPendingIntent);
manager.SetAlarmClock(alarmInfo, ringPendingIntent);
UPDATE 2:
I've found one more important line in the same dumpsys alarm log:
Next alarm clock information:
user:0 pendingSend:false time:1484035080736 = 2017-01-10 08:58:00 = -58m6s876ms
..which means the alarm was missed, but the system didn't even try to present it, the logcat didn't show any activity at all around the 8:58. And still not clear why would the system reschedule the alarm to 20:58.

Related

No alarm clock sign in Android 10 when scheduling alarm with AlarmManager#setAlarmClock

My app is an alarm clock app, and I am setting an alarm in the following way:
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity_AlarmsList.this, alarmID, intent, 0);
ZonedDateTime zonedDateTime = ZonedDateTime.of(alarmDateTime.withSecond(0), ZoneId.systemDefault());
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000, pendingIntent), pendingIntent);
where alarmDateTime is a LocalDateTime object containing the alarm date and time.
On Android M and N (real devices), an alarm clock sign appears on the status bar as soon as an alarm is set. But on my Samsung Galaxy M31s (Android Q), no alarm sign appears! But the alarm clock sign appears when I run the app on an Android Q emulator.
In the Samsung Galaxy M31s, if I execute adb shell dumpsys alarm, I get the following output:
Next wake from idle: Alarm{c9c59a type 0 when 1603225320000 in.basulabs.shakealarmclock}
tag=*walarm*:in.basulabs.shakealarmclock.DELIVER_ALARM
type=0 expectedWhenElapsed=+48s986ms expectedMaxWhenElapsed=+48s986ms whenElapsed=+48s986ms maxWhenElapsed=+48s986ms when=2020-10-21 01:52:00.000
window=0 repeatInterval=0 count=0 flags=0x3
Alarm clock:
triggerTime=2020-10-21 01:52:00.000
showIntent=PendingIntent{f26a278: PendingIntentRecord{9c3ed51 in.basulabs.shakealarmclock broadcastIntent}}
operation=PendingIntent{860e9b6: PendingIntentRecord{9c3ed51 in.basulabs.shakealarmclock broadcastIntent}}
which is what I want. So the alarm is set, and indeed the alarm rings too, but no alarm clock sign appears.
What is the problem?

one extra alarm is set by Alarm Manager

When I set alarm using alarm manager, and extra alarm is set. I don't know why that happens.
The results of "adb shell dumpsys alarm " shows that there is one extra alarm, here is the result:
RTC_WAKEUP #0: Alarm{6d76e0b type 0 when 1597496400982 ir.zima.schema}
operation=PendingIntent{4c364e8: PendingIntentRecord{360d701 ir.zima.schema broadcastIntent}}
RTC_WAKEUP #0: Alarm{5f1e9e1 type 0 when 1912856649822 ir.zima.schema}
operation=PendingIntent{2495306: PendingIntentRecord{d285dc7 ir.zima.schema broadcastIntent}}
Next wake from idle: Alarm{6731a0e type 0 when 1596440300037 ir.zima.schema}
operation=PendingIntent{5c68c2f: PendingIntentRecord{e088129 ir.zima.schema broadcastIntent}}
The strange part is that even after I deleted the method call (literally there are no alarms set by me anymore) adb still says there is one alarm set:
RTC_WAKEUP #0: Alarm{63e6995 type 0 when 1912857022688 ir.zima.schema}
operation=PendingIntent{afc8aaa: PendingIntentRecord{943069b ir.zima.schema broadcastIntent}}
Next wake from idle: Alarm{6731a0e type 0 when 1596440300037 ir.zima.schema}
operation=PendingIntent{5c68c2f: PendingIntentRecord{e088129 ir.zima.schema broadcastIntent}}
This is the method where I set my Alarm in AlarmManagerHelper.class:
public static void setAlarm(Context context, int requestCode, int hour, int minute){
AlarmManager am =( AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context//same activity should be used when canceling the alarm
, AlarmReceiver.class);
intent.setAction("android.intent.action.NOTIFY");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar time = getTime(hour, minute);
//set Alarm for different API levels
if (Build.VERSION.SDK_INT >= 23){
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),pendingIntent);
}
else{
am.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),pendingIntent);
}
}
Then I call this method like this:
Context context = getActivity().getApplicationContext();
AlarmManagerHelper.setAlarm(context,
AlarmManagerHelper.FIRST_ALARM_REQUEST_CODE,
17,30);
Do you have any idea what might be going wrong?!
What are the long numbers? I'm guessing those are the milliseconds since epoch?
1597496400982 - GMT: Saturday, 15. August 2020 13:00:00.982 (yesterday)
1912856649822 - GMT: Tuesday, 13. August 2030 13:04:09.822 (in ten years)
1912857022688 - GMT: Tuesday, 13. August 2030 13:10:22.688 (in ten years)
Maybe you created those alarms 3 days ago (13. August) by accident or the system created them for some weird reason. Considering the lifespan of smartphone, they won't be triggered anyway.
If you want to debug it some more you could set the system clock to that time and try to get more information from the Intent which is passed to your BroadcastReceiver.

setExactAndAllowWhileIdle is almost triggered exactly

I searched the Internet for an answer but have not found anything.
I wrote an app where every 17 minute an SMS must be sent ( every 17 minute means: 10:17:00 and not at 10:17:49 )
Therefore, I use an AlarmManager with setExactAndAllowWhileIdle, and AlarmManager.RTC_WAKEUP.
Long whenToFireOff = treeMapInstance.getFirstEntryInTreeMap().getKey();
Calendar alarmCal = Calendar.getInstance();
alarmCal.set(Calendar.HOUR_OF_DAY, treeMapInstance.convertMillisecondsToHour(whenToFireOff));
alarmCal.set(Calendar.MINUTE, treeMapInstance.convertMillisecondsToMinutes(whenToFireOff));
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmCal.getTimeInMillis(), pi);
However, it gets always triggered round about 10:17:49, and the next scheduled alarm also at 10:34:49, and so on.
I calculated my next alarm correctly. I am absolutely 100% sure :)
Is setExactAndAllowWhileIdle not as exactly as it is written in the documentation?
Thanks

setAlarmClock() not working in dozo mode for few devices like OPPO phones

Inside my application I need to set alarms and when they fire I'm trying to trigger them in my Receiver.
In order to deal with Dozo mode from marshmallow 6.0, if the device version is greater or equal to 6.0 I'm using alarmManger.setAlarmClock() method. This works fine is almost all the devices except OPPO devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(fireDate.getTime(), pendingIntent);
alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
//alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate.getTime(), pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireDate.getTime(), pendingIntent);
}
======================================================================
from Terminal, I got logs using : adb shell dumpsys alarm.
1. for other brands (my app alarmManager.setAlarmClock() method result):
Batch{b778e8c num=1 start=35988972 end=35988972 flgs=0x3}:
RTC_WAKEUP #0: Alarm{b9e89d5 type 0 when 1547501400600 ######.######}
tag=*walarm*:######.######.Reminder
type=0 whenElapsed=+9h49m0s741ms when=2019-01-15 03:00:00
window=0 repeatInterval=0 count=0 flags=0x3
Alarm clock:
triggerTime=2019-01-15 03:00:00
showIntent=PendingIntent{3dc5ea: PendingIntentRecord{d66ec2a ######.###### broadcastIntent}}
operation=PendingIntent{996c0db: PendingIntentRecord{d66ec2a ######.###### broadcastIntent}}
the batch is unique and the flag value is set to 0x3, this is how it will be for alarmClocks.
For most of the brands the result is same, but in oppo the flag is set to 0x8
2. for oppo phone (my app alarmManager.setAlarmClock() method result ):
Batch{b778e8c num=1 start=35988972 end=35988972 flgs=0x8}:
RTC_WAKEUP #0: Alarm{b9e89d5 type 0 when 1547501400600 ######.######}
tag=*walarm*:######.######.Reminder
type=0 whenElapsed=+9h49m0s741ms when=2019-01-15 03:00:00
window=0 repeatInterval=0 count=0 flags=0x8
Alarm clock:
triggerTime=2019-01-15 03:00:00
showIntent=PendingIntent{3dc5ea: PendingIntentRecord{d66ec2a ######.###### broadcastIntent}}
operation=PendingIntent{996c0db: PendingIntentRecord{d66ec2a ######.###### broadcastIntent}}
3. for oppo phone (oppo default alarm app):
Batch{3450b8d num=1 start=464582537 end=464582537 flgs=0x9}:
RTC_WAKEUP #0: Alarm{32536c5 type 0 when 1547690400000 ######.###### whenElapsed 464582537 windowLength 0 maxWhenElapsed 464582537 repeatInterval 0 action }
tag=*walarm*:######.######.ALARM_ALERT
type=0 whenElapsed=+17h4m45s187ms when=2019-01-17 07:30:00
window=0 repeatInterval=0 count=0 flags=0x9
Alarm clock:
triggerTime=2019-01-17 07:30:00
showIntent=PendingIntent{ee6881a: PendingIntentRecord{97c18f0 ######.###### startActivity (whitelist: 902f275:+30s0ms)}}
operation=PendingIntent{3bc0f4b: PendingIntentRecord{2bd8d28 ######.###### broadcastIntent}}
Inside opp phones to get alarms it should be a single batch and flag should be set to 0x9
Conclusion
OTHER Phones: flag = 0x3 is required to trigger it as alarm, alarmManager.setAlarmClock() is setting the flag = 0x3, works fine.
OPPO Phones: flag =0x9 is required to trigger it as alarm. But, alarmManager.setAlarmClock() is setting the flag = 0x8.
What method should I call to make the flag = 0x9 in oppo devices?

AlarmManager.RTC_WAKEUP not working for some devices

I am using AlarmManager in my application to set alarm for a particular time. I have used AlarmManager.RTC_WAKEUP to set the alarm. When I am testing the same it's working on number of device like Lg optimus, Sony Xperia etc. But while testing the same app in Samsung Galaxy S3 I found that alarm is not working. I am still unable to understand why is this happening.
I am using following code to set alarm :-
// create the object
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis(), PendingIntent.getBroadcast(getActivity(),reminderId, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Someone please help me to solve this strange problem. Any help would be appreciable.
Thank you
I'm not sure but it sounds the problem is with the way you set cal1's time and maybe different timezone.
I'm not sure what the particular issue you are having is, but you could try to replace cal1.getTimeinMillis() with System.currentTimeMillis() + your time delta in ms. This works consistently for me.
Is your testing getting tripped up by the long-time Android OS bug https://code.google.com/p/android/issues/detail?id=2880 ? If the clock gets set backwards, Intent.ACTION_DATE_CHANGED broadcast won't be delivered until the clock catches up to what was going to be the alarm time. (Reboot to reset this broken state.) That's because this Intent is based on an AlarmManager alarm which does not get updated when the clock changes.
That situation is pretty rare in normal use. But it occurs frequently when testing alarm code.
If you're testing an AlarmManager alarm by adjusting the clock, AFAIK any alarm set to a target Calendar on any Android device will get messed up by adjusting the clock. You can work around it by rescheduling your alarm whenever the clock gets adjusted. This assumes the system properly broadcasts Intent.ACTION_TIME_CHANGED so you can tell when the clock gets adjusted. (That Intent is misnamed. It's not like Intent.ACTION_DATE_CHANGED which occurs when the date changes due to time passing.)
I think you have problem in your "cal1" object;
When you set time in calendar object, actually setting Month, you should not use exact numeric value of month;
ex: - April = 4, you must use 3 instead of 4
I face this problem in API Level 19, but don't know about others.
This Question may give you better explanation.Why month give wrong value
public Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,2014);
c.set(Calendar.MONTH, 03);//One minus from actual month numeric value
c.set(Calendar.DATE, 24);
c.set(Calendar.HOUR_OF_DAY,16 );
c.set(Calendar.MINUTE, 39);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);

Categories

Resources