Implementing an alarm every 5 days, code correct?

2019-09-11 22:47发布

I am trying to set an alarm every 5th day of the week and the 24th hour of that day.

Here is the code i am using. Ive been reading over the Calendar and AlarmManager docs, a

and here is what i have came up with.

 String alarm = Context.ALARM_SERVICE;
     //Alert for game covers
     am = (AlarmManager)context.getSystemService(alarm);
     calendar = Calendar.getInstance();
     calendar.set(Calendar.DAY_OF_WEEK, 5);
     calendar.set(Calendar.HOUR_OF_DAY, 23);
     calendar.set(Calendar.MINUTE, 0);
     calendar.set(Calendar.SECOND, 0);
    Intent Aintent = new Intent("REFRESH_THIS");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pi);

Is this correct for what i want to do?

1条回答
别忘想泡老子
2楼-- · 2019-09-11 23:14

To get a Calendar instance, that points to a date 5 days in the future, you take the current date and add 5 days like this:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 5);

Then you set your alarm:

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);
查看更多
登录 后发表回答