private void setAlarm(int trigId, TimeTrigDesc desc) { // Cancel the pending intent and the existing alarm first cancelAlarm(trigId, desc.toString()); Log.v(TAG, "TimeTriggerService: Attempting to set trigger " + trigId); Intent i = createAlarmIntent(trigId, desc.toString()); PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); long alarmTime = getAlarmTimeInMillis(trigId, desc); if (alarmTime == -1) { Log.v(TAG, "TimeTriggerService: No valid time found for " + trigId); return; } /* Convert the alarm time to elapsed real time. * If we dont do this, a time change in the system might * set off all the alarms and a trigger might go off before * we get a chance to cancel it */ long elapsedRT = alarmTime - System.currentTimeMillis(); if (elapsedRT <= 0) { Log.v( TAG, "TimeTriggerService: negative elapsed realtime - " + "alarm not setting: " + trigId); return; } Log.v(TAG, "TimeTriggerService: Setting alarm for " + elapsedRT + " millis into the future"); mAlarmMan.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + elapsedRT, pi); }