Implementing an alarm every 5 days, code correct?

前提是你 提交于 2020-01-17 04:05:26

问题


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:


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


来源:https://stackoverflow.com/questions/7458239/implementing-an-alarm-every-5-days-code-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!