【问题标题】:Set notification for specific time设置特定时间的通知
【发布时间】:2021-02-08 20:22:57
【问题描述】:

我目前创建了一个通知,当检查开关时显示没有问题,但是我想创建一个 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
                    }
               }

【问题讨论】:

    标签: java android-studio android-notifications android-calendar android-switch


    【解决方案1】:

    您可以使用警报管理器

    Intent myIntent = new Intent(ThisApp.this , myService.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(ThisApp.this, 0, 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 类中!

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多