Reputation: 9
I am using this code and my AlarmManager fires immediately as the time passed to fire has still time to fire I am printing it on LOG so you can see.
public void SetAlarm(Context context, Date date, List<String> temp) {
Log.d("Alarm Set", "Entered");
Log.d("Alarm Set", date.getHours() + " " + date.getMinutes());
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
i.putStringArrayListExtra("list", (ArrayList<String>) temp);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_ONE_SHOT);
date.setMinutes(date.getMinutes() - 5);
Calendar rightNow = Calendar.getInstance();
long offset = rightNow.get(Calendar.ZONE_OFFSET)
+ rightNow.get(Calendar.DST_OFFSET);
long sinceMidnight = (rightNow.getTimeInMillis() + offset)
% (24 * 60 * 60 * 1000);
long time_in_milis = (1000 * 60 * ((date.getHours() * 60) + date
.getMinutes())) - sinceMidnight;
Log.d("Alarm Time in Mili Seconds", "" + time_in_milis);
am.set(AlarmManager.RTC_WAKEUP, time_in_milis * 10000, pi);
}
Upvotes: 0
Views: 487
Reputation: 6866
If I'm reading your code correctly, you're calculating time_in_milis
[sic] as a time difference, your desired time in relation to "right now". However, set()
expects an absolute timestamp.
In other words, you're supplying a pretty low timestamp, effectively scheduling your alarm in the past.
For a simple test scenario, try passing System.currentTimeMillis() + 60000
-- your alarm should fire about a minute later (remember that set()
sets inexact alarms).
Upvotes: 1