Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

Alarm manager triggered too many times

I have a problem regarding Alarm manager in Android. I have the following code snippet to set an alarm that should be fired each week(once).

  // Add the time and set when the notification will be triggered
    Calendar setCalendar = item.getDate();
    calendar.set(Calendar.MINUTE,setCalendar.get(Calendar.MINUTE)+10080);

   //Create a new alarm intent
   Intent alarmIntent = new Intent(ApplicationUtils.getApplicationContext(), AlarmReceiver.class);


   PendingIntent sender = PendingIntent.getBroadcast(ApplicationUtils.getApplicationContext(), requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get the AlarmManager service
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.ELAPSED_REALTIME, sender);

And I have to following problem. When the week is changed, the notifications are coming up and they never stop. Does anybody have any idea how can I set the calendar so the alarms are triggered once a week?

Thanks, Arkde

Upvotes: 0

Views: 1007

Answers (2)

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

Check out the Code it help you.

 Intent intent_for_every_second = new Intent(Activity.this, Notifier.class);
 pendingIntent_for_every_second = PendingIntent.getBroadcast(Activity.this, 0,    intent_for_every_second,0);
 AlarmManager alarmManager_for_every_second = (AlarmManager) getSystemService(ALARM_SERVICE);
  alarmManager_for_every_second.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 1000,pendingIntent_for_every_second);

Upvotes: 1

Ran
Ran

Reputation: 4147

You're third parameters in setRepeating is incorrect. It should be an interval between the repeating alarms in milliseconds.

A week would be: 1000 * 60 * 60 * 24 * 7 .

http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)

Upvotes: 1

Related Questions