[英]Fire notification with AlarmManager
我正在使用AlarmManager觸發通知。 我希望該通知在上午10:30觸發,並每24小時重復一次。 打開應用程序時我不希望收到通知。
我正在發布代碼,主要問題是它也會在晚上10點30分(如果電話時區為12小時)在夜間觸發警報。 我通過修改yest來檢查此代碼,並在10:30、1、4、7收到了yestn警報。
請幫助我解決問題,我正試圖從很長一段時間解決問題。
代碼:
Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR_OF_DAY, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if (intendedTime >= currentTime) {
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
} else {
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
}
嘗試使用:
firingCal.set(Calendar.HOUR_OF_DAY, 10);
因為這是24小時制中的小時數
似乎您也忘記了在else語句中添加一天,因為if和else現在完全一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.