每周重复Manager报警

时间:2015-12-12 19:44:22

标签: android

我想每周做一个特定的任务。 所以我使用了一个报警管理器 当用户点击按钮alarmManager运行时 打击代码正常工作:

       public void set_alarm(int reapte)
{
    Context context=getBaseContext();
    Calendar calendar=Calendar.getInstance();
//  calendar.set(Calendar.HOUR_OF_DAY, 20); // For 1 PM or 2 PM
    //calendar.set(Calendar.MINUTE, 41);
    Intent intent= new Intent(context, MyService.class);
    intent.putExtra("size", reapte);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getService(context, 0,
              intent ,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY*7, pi);


    }

我遇到的问题。   当用户点击时报警管理器运行同一时刻。 下周将会运行。 我想先从下周开始报警 .sorry我的语言不好

2 个答案:

答案 0 :(得分:0)

试试这个 您可以设置闹钟开始的日期。

<a class="btn btn-success" id="btnid" onclick="javascript:theFunction();" href="#">Text</a> <script type="text/javascript"> function theFunction () { event.preventDefault(); // add your ajax call to the controller here e.g $.ajax({ url: "http://site/controllerName/parameter" }).done(function() { // action when the controller call returns }); } </script> 其中sunday = 1,依旧等于= 7

并设置从设置闹钟的当前时间开始前2分钟的时间,因此它将在下周的同一天

答案 1 :(得分:0)

我终于找到了答案

WEEK_OF_MONTH值介于1和4之间。

所以 我使用下面的代码:

    int week=calendar.get(Calendar.WEEK_OF_MONTH);
    int day=calendar.get(Calendar.DAY_OF_WEEK);
    int hour=calendar.get(Calendar.WEEK_OF_YEAR);
    int minute= calendar.get(Calendar.MINUTE);

    calendar.set(Calendar.DAY_OF_WEEK, day);
    calendar.set(Calendar.HOUR_OF_DAY, hour); 
    calendar.set(Calendar.MINUTE,minute);

    int next_week=week+1;

    if(next_week>4)
         next_week=1;
    calendar.set(Calendar.WEEK_OF_MONTH, next_week); 

   //other code