每天重复特定时间的通知

时间:2017-02-07 20:40:10

标签: java android time notifications alarmmanager

我希望应用程序首先在特定时间(早上7点)检查某些内容,然后,如果条件为真,则发送通知,即使应用程序未激活,甚至是#34;仅"在后台运行。

这是现在的代码(在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。一般来说它看起来怎么样?

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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" />