每日闹钟问题,Calendar.DATE和Calendar.DAYS_OF_MONTH的区别

时间:2016-08-23 06:23:40

标签: android alarmmanager

我正在尝试实现每天同时通知的闹钟。它工作正常。但是当我再次设置闹钟时,它会在没有等待时间的情况下当场通知我,因为已经通过了设置闹钟的时间。但是,当我将Calendar.DATE更改为Calendar.DAYS_OF_MONTH时,它不会通知我。所以任何人都可以帮助我,我错了。我在下面分享我的代码。

       public void setNotification() {
       PendingIntent pendingIntent = createPendingIntent();

        Calendar calendarToSet=Calendar.getInstance();
        Calendar currentTime=Calendar.getInstance();
        calendarToSet.set(Calendar.HOUR,10);
        calendarToSet.set(Calendar.MINUTE,0);
        calendarToSet.set(Calendar.SECOND,0);
        calendarToSet.set(Calendar.AM_PM,Calendar.AM);

        if(calendarToSet.before(currentTime))
        {
            calendarToSet.set(Calendar.DATE,1);
        }


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

       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendarToSet.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
       // Toast.makeText(MyAppManager.context, "set", Toast.LENGTH_LONG).show();
}

        public PendingIntent createPendingIntent() {
        Intent intent = new Intent(MyAppManager.context, TaffaquhFiddinBroadCastReceiver.class);
        intent.putExtra("key", "What_to_say_upon_completing_ablution");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MyAppManager.context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;

    }

我想要做的是如果时间过去就设置第二天的闹钟,所以请帮忙。

Calendar.DATECalendar.DAYS_OF_MONTH之间有什么区别?

1 个答案:

答案 0 :(得分:3)

问题是您设置 Calendar个实例的日期字段,而不是每天添加

更改以下内容:

calendarToSet.set(Calendar.DATE, 1);

对此:

calendarToSet.add(Calendar.DATE, 1);
  

Calendar.DATECalendar.DAYS_OF_MONTH之间的区别是什么?   DATE

它们是相同的,DAY_OF_MONTH只是Calendar的同义词。

来自public static final int DATE = 5; public static final int DAY_OF_MONTH = 5; 类的源代码:

    <!DOCTYPE html>
        <html>
        <body>

        <p>When you submit the form, a function is triggered which alerts some text.</p>

        <form action="demo_form.asp" id="form">
          Enter name: <input type="file" name="fname" id="form_file" onchange=" return myFunction()">
          <input type="submit" value="Submit" id="submit">
        </form>

        <script>
        function myFunction() {
             var file = document.getElementById("form_file");
             var file_name = file.value;
             var extension = file_name.split('.').pop().toLowerCase();
             var size      = file.files[0].size;
             var allowedFormats = ["jpeg", "jpg", "pdf", "tif"];

              if(!(allowedFormats.indexOf(extension) > -1)){
               alert("Enter a jpg/jpeg/pdf/tif file");

               document.getElementById("submit").disabled = true;
               return false;              
              } else if( ((size/1024)/1024) > 15){
                 alert("Your file should be less than 15MB");
                 return false;
              } else {
               document.getElementById("submit").disabled = false;   
              }
        }
        </script>
        </body>
        </html>

如您所见,它们代表相同的价值。