AlarmManager.RTC_WAKEUP not working for some devices
Asked Answered
T

4

6

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

Tilla answered 7/3, 2014 at 9:26 Comment(6)
Best to post the code where you create cal1 too.Melly
Btw can you define what " is not working" means? It doesn't fire at all or fires at the wrong time? I've testes on the S3 and generally it works the same as any other device.Melly
@confused_at_times, It doesn't fire at all that is the problem .....Tilla
Are you sure the calendar is set to the right time? You can output the time to the logcat, is it the correct time?Jeanettajeanette
@SalmanKhan i have the same problem did u find any solution? Thanks.Stetson
@KalpeshLakhani, not yet ...Tilla
H
3

I'm not sure but it sounds the problem is with the way you set cal1's time and maybe different timezone.

Harald answered 7/3, 2014 at 10:5 Comment(1)
But the devices on which I am testing the application are all on the same time zone.Tilla
M
0

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.

Melly answered 7/3, 2014 at 12:52 Comment(5)
But I do need the cal1.getTimeinMillis() because it is the user defined time, I don't want System time....Tilla
Sorry, part if my post disappeared. Updated now. It may not be an acceptable solution depending on what your app does.Melly
This is not working even on the devices on which the previous code was working .......Tilla
Which OS version is on the SGS3? Remember that with Android Kit Kat, alarms set using Alarm Manager.set will not be exact, but grouped into parcels to reduce battery consumption. When I'm home tonight, I'll check my source code.Melly
None of them are on KitKat, most of them are on Jelly Bean or ICS.Tilla
T
0

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

Tilt answered 1/4, 2014 at 6:42 Comment(2)
But what if I am setting clock to the future time, the problem will remains same for some devices ??Tilla
Assuming this is the problem that you're having, AFAIK the AlarmManager problem happens on all Android devices where a Calendar-based alarm gets messed up by adjusting the clock. I'm unsure about forwards vs. backwards adjustments except for Intent.ACTION_DATE_CHANGED which is implemented in terms of an alarm. I believe you can work around it by rescheduling your alarm when the clock gets adjusted or scheduling your alarm after adjusting the clock.Tilt
M
-1

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);
Monaural answered 1/4, 2014 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.