簡體   English   中英

每天重復特定時間的通知

[英]Notification on specific time repeated every day

我希望應用程序首先在特定時間(早上 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。 一般情況下它應該是什么樣子的?

提前致謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM