[英]Schedule Alarm Manager daily
我在設置Android AlarmManager每天執行時遇到一些問題。 用戶可以設置自己的特定時間,並且應該每天在該時間運行。 這是代碼:
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(MainActivity.this,
NotificationBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Make sure, there is no active Alarm
alarmManager.cancel(pendingIntent);
if(notificationEnabled) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.PM);
cal.set(Calendar.HOUR_OF_DAY, notificationHour);
cal.set(Calendar.MINUTE, notificationMinute);
cal.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
現在我不知道為什么這行不通。 我曾經將警報設置為10:05,並且在晚上觸發了警報。 該警報應每天在特定時間觸發(當然,如果僅啟用了警報)。 從當天開始。 因此,如果我將其設置為幾分鍾或幾小時后,它也應該在今天和隔日運行,直到取消警報為止。#
提前致謝
我認為您從Calender獲得了錯誤的Unixtimestamp。
嘗試這個
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.