开发者

Setting alarm using calendar.getTimeInMillis() not working

Trying to fire up an alarm at a specific time, like this:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 17);开发者_开发问答
calendar.set(Calendar.MINUTE, 54);

AlarmManager alarmManager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);   
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 30*1000, mAlarmSender);

Doesn't seem to be working. I'm logging onCreate() in my Service and it doesn't seem to be getting called ever.

EDIT - I tried switching to calendar.set(Calendar.HOUR_OF_DAY, 17), which I believe is the right way to do it because that's the 24 hr hour.

But it still isn't working... ;)

EDIT It's working now. I just jumped the gun a bit. Apparently the alarm isn't 100% exact.


Calendar object must be set to (Calendar.HOUR_OF_DAY) as opposed to (Calendar.HOUR) when you're using a 24 hr hour.


Use like,

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 54);

AlarmManager alarmManager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);   
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 30*1000, mAlarmSender);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜