Reputation: 4427
I've worked with alarms many times, but I can't for the life of me get this to trigger... What am i doing wrong? I've tried triggering Services, BroadcastReceivers, everything. I've tried triggering it from an earlier time and in the future (5 seconds from now). As far as i know it's being set, It's not throwing an error and my log is firing... But nothing in AlarmReceiver is getting triggered.
public void setAlarm() {
Calendar exp = Calendar.getInstance();
exp.set(Calendar.SECOND, 5);
AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, exp.getTimeInMillis(), PendingIntent.getService(mContext, 0, new Intent(mContext, AlarmReceiver.class), 0));
Log.i("", "Alarm set for " + DateFormat.format("MMMM dd, yyyy hh:mma", exp.getTime()));
}
}
public class AlarmReceiver extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.d("", "onBind Tiggered");
return null;
}
public void onCreate() {
Log.d("", "onCreate Triggered");
}
}
Upvotes: 0
Views: 65
Reputation: 6409
The time will only be in the future if you run this in the first 5 seconds of a minute.
You should use calendar.add(field, value);
to add to the time.
If you're in a time zone except GMT, that might also be throwing Calendar off.
To set an alarm for 5 seconds time, it easiest to do it like this:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
Upvotes: 0