我的應用程序執行以下代碼:到AlarmManager.setRepeating()第二呼叫會在錯誤的時間交付
// Check intakes every 15 minutes
Intent i = new Intent(this, IntakeReceiver.class);
i.putExtra("TYPE", "TAKEIN_CHECK");
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
AlarmManager am = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
Calendar cal = Calendar.getInstance();
int offset = 1000 * 60 * 15; // 1000 * 60 * 15 (15 minutes)
long nowLastRounded = cal.getTimeInMillis() - (cal.getTimeInMillis() % offset);
am.setRepeating(AlarmManager.RTC_WAKEUP, nowLastRounded + offset, offset, pi);
// Also set alarm for 11:50 every day to notify about low stock
Intent i2 = new Intent(this, LowStockReceiver.class);
i2.putExtra("TYPE", "STOCK_CHECK");
PendingIntent pi2 = PendingIntent.getBroadcast(getApplicationContext(), 1, i2, 0);
long interval_day = AlarmManager.INTERVAL_DAY;
long interval_hour = AlarmManager.INTERVAL_HOUR;
int offset2 = 1000 * 60 * 60 * 11 + 1000 * 60 * 50; // 11:50
final int tzOffset = TimeZone.getDefault().getOffset(System.currentTimeMillis());
offset2 -= tzOffset;
nowLastRounded = cal.getTimeInMillis() - (cal.getTimeInMillis() % interval_day);
am.setRepeating(AlarmManager.RTC_WAKEUP, nowLastRounded + offset2, interval_hour, pi2);
的問題是,LowStockReceiver.onReceive()
被稱爲每隔15分鐘(如IntakeReceiver.onReceive()
),但它僅應從11:50開始每小時調用一次。
我檢查了nowLastRounded + offset2
的值,它等於當地時間今天11:50轉換爲GMT。所以它應該在11:50,12:50,13:50等運行(僅用於當前的測試目的)。
任何人都知道我可能做錯了什麼?
謝謝!
我正在使用ID 0和ID 1我不是嗎? 或者你不是在談論requestCode? – profoX