设置特定时间的通知

问题描述

我目前创建了一个通知,在选中开关时显示没有问题,但是我想创建一个 if 语句来设置显示通知的当前时间。我已经尝试了以下方法,但是测试时在下午 3 点 30 分没有显示通知,我是否遗漏了什么或我该怎么做?

    public void onCheckedChanged(CompoundButton buttonView,boolean notificationSwitchisChecked) {
          if(notificationSwitchisChecked){
               Calendar.getInstance();
               if (Calendar.HOUR_OF_DAY == 15 && Calendar.MINUTE == 30 && Calendar.SECOND == 0){
                 sendNotification(); // this method displays the notification no problem when checked
                    }
               }

解决方法

您可以使用闹钟管理器

Intent myIntent = new Intent(ThisApp.this,myService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this,myIntent,0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,12);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24*60*60*1000,pendingIntent);  //set repeating every 24 hours

并将通知放在 myService 类中!