【问题标题】:Notification on specific time repeated every day每天重复的特定时间通知
【发布时间】:2017-02-07 20:40:09
【问题描述】:

我希望应用程序首先在特定时间(早上 7 点)检查某些内容,然后,如果条件为真,则发送通知,即使应用程序未处于活动状态或什至“仅”在背景。

这是现在的代码(在 MainActivity.java 中):

Intent intent_notification = new Intent(getApplicationContext() , NotificationClass.class);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent_notification, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);

我不确定 NotificationClass.class。它一般看起来如何?

提前致谢。

【问题讨论】:

    标签: java android time notifications alarmmanager


    【解决方案1】:

    NotificationClass.class 将是警报广播接收器。警报服务将在预定时间调用此接收器。

    public class NotificationClass extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //check something at a scheduled time
            if(condition is true){ 
                 sendNotification();
            }
        }
    }
    

    在你的清单中声明NotificationClass.class

    <receiver android:name=".NotificationClass"
              android:process=":remote" />
    

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多