Salman Khan
Salman Khan

Reputation: 2832

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

Upvotes: 6

Views: 3900

Answers (4)

shalin
shalin

Reputation: 452

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

Upvotes: -1

Jerry101
Jerry101

Reputation: 13467

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

Upvotes: 0

confused_at_times
confused_at_times

Reputation: 545

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.

Upvotes: 0

Dariush Malek
Dariush Malek

Reputation: 914

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

Upvotes: 3

Related Questions