时间:2017-01-09 13:55:14

标签: android calendar alarmmanager

我在Android中使用AlarmManager对闹钟进行编码。 我想在特定的日期和时间设置它。为了检查它是否有效,我在今天的日期(2017年1月9日)尝试了代码。 问题是警报没有被触发。相反,如果我将Calendar.DAY_OF_MONTH更改为8而不是9,它就会起作用,好像DAY_OF_MONTH从0开始就像Calendar.MONTH一样,但我知道它不会从0开始。

为什么会这样?这是我的警报代码:

private class AppointmentAlarmSetter extends AsyncTask<String, Void, Boolean>
    {
     @Override
     protected Boolean doInBackground(String... strings)
        {
         // The Alarm's Request Code
         int currentID = Constants.APPOINTMENT_ALARM_ID;

         // Start setting the alarm for current appointment
         Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);

         // put the RequestCode ID as intent's extra, in order to identify which alarm is triggered
         alarmIntent.putExtra("request_code", currentID);

         PendingIntent pendingIntent = PendingIntent.getBroadcast
                        (
                         context.getApplicationContext(),
                         currentID,
                         alarmIntent,
                         PendingIntent.FLAG_CANCEL_CURRENT
                        );

         Calendar calendar = Calendar.getInstance();

         calendar.setTimeInMillis(System.currentTimeMillis());

         // Specify the date/time to trigger the alarm
         calendar.set(Calendar.YEAR, 2017);
         calendar.set(Calendar.MONTH, 0);
         calendar.set(Calendar.DAY_OF_MONTH, 9);
         calendar.set(Calendar.HOUR_OF_DAY, 14);
         calendar.set(Calendar.MINUTE, 16);
         calendar.set(Calendar.SECOND, 0);

         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

         // Set the exact time to trigger the alarm
         alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

         return true;
        }
    }

2 个答案:

答案 0 :(得分:1)

我发现了错误......在我的代码的另一部分中,我有两行:

Calendar appointmentDate = calendar;
appointmentDate.add(Calendar.DAY_OF_MONTH, 1);

通过评论它们,一切正常。

答案 1 :(得分:0)

使用它:

Calendar now = Calendar.getInstance();
        Calendar wakeupcall = Calendar.getInstance();
        wakeupcall.setTimeInMillis(System.currentTimeMillis());
        wakeupcall.set(Calendar.HOUR_OF_DAY, 18);
        wakeupcall.set(Calendar.MINUTE, 30);

        if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
            _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
        else
            _alarm=wakeupcall.getTimeInMillis();


        al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        notif= new Intent(this,TestNotifyService.class);
        fintent = PendingIntent.getService(this,0,notif,0);

        if (SDK_INT < Build.VERSION_CODES.KITKAT) {
            al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
        }
        else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
            al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
        }
        else if (SDK_INT >= Build.VERSION_CODES.M) {
            al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
        }