1

I set an AlarmManager to open notification every 24 hours.
But every time I open the activity, the notification starts.
So how to set it not to show the notification when the activity is already opened?

This is my AlarmManager code:

AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        Intent intent = new Intent(this, myService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
        Calendar calendar = Calendar.getInstance();
        long when = calendar.getTimeInMillis();         // notification time
        calendar.set(Calendar.HOUR_OF_DAY, 20);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.setRepeating(AlarmManager.RTC, when, 24*60*60*1000, pendingIntent);  //every 24 hours

Thanks ,

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
A. M. Sid
  • 67
  • 1
  • 4
  • Your when variable points to the current time, hence the alarm will be fired right at execution of that part. Change `when` to `calendar.getTimeInMillis()`. Your calendar variable is set to the hour 20 of the day and repeating the day after. Here is a working copy:http://stackoverflow.com/questions/21461191/alarmmanager-fires-alarms-at-wrong-time/21461246#21461246 – Skynet Feb 02 '14 at 11:30
  • So How to change that ? – A. M. Sid Feb 02 '14 at 11:32
  • How to set you the best answer now :) – A. M. Sid Feb 02 '14 at 11:36
  • You can accept my answer below, by clicking the tick mark :) – Skynet Feb 02 '14 at 11:38

1 Answers1

0

Your when variable points to the current time, hence the alarm will be fired right at execution of that part. Change when to calendar.getTimeInMillis(). Your calendar variable is set to the hour 20 of the day and repeating the day after. Here is a working copy

Community
  • 1
  • 1
Skynet
  • 7,820
  • 5
  • 44
  • 80