问题
I have a notification at a specific time, see my code:
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent0 = new Intent(this, AlarmReceiver_maandag_1e.class);
intent0.putExtra("uur", "1e");
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
//set timer you want alarm to work (here I have set it to 8.30)
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 8);
timeOff9.set(Calendar.MINUTE, 30);
timeOff9.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent0);
This works perfect except one thing.
The notification is set for 8:30 with an alarm manager If I launch the app after 8:30, 9:00 for example, the notification still shows..
Is there a possibility that the alarm goes off at 8:30 but not later?
Edit
For anyone having the same problem, here is the solution:
Put this in your broadcastreceiver:
long current_time = System.currentTimeMillis();
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 8);
timeOff9.set(Calendar.MINUTE, 35);
timeOff9.set(Calendar.SECOND, 0);
long limit_time = timeOff9.getTimeInMillis();
if (current_time > limit_time) {
//nothing
} else {
//show notification
}
回答1:
You can also try this method of Alarm Manager:
public void setExact (int type, long triggerAtMillis, PendingIntent operation)
But it requiers API level 19.
来源:https://stackoverflow.com/questions/15744654/notification-at-specific-time