Search code examples
androidalarmmanagerandroid-notifications

android set an alarm with only one repeat


at the moment i set an alarm in my android app like this:

scheduleNotification(getNotification("HELLO WORLD"), 5000, 1);

 private void scheduleNotification(Notification notification, int delay, int id) {

    Intent notificationIntent = new Intent(this, ShowNotification.class);
    notificationIntent.putExtra("NOTIFICATION_ID", id);
    notificationIntent.putExtra("NOTIFICATION", notification);

    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + delay,
            PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_ONE_SHOT));
}

private Notification getNotification(String content) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentText(content);
    builder.setSmallIcon(R.drawable.ic_notification_appicon);
    return builder.getNotification();
}

is there an way, to set an alarm, which will be active one time and only one time 2 hours later?


Solution

  • No set methods in AlarmManager does that. You should have your own counter to stop it after N times.