-1

I'm trying to trigger an alarm after seven days when the user launches the app. I have tried to set the alarm to trigger in following way:

Intent i = new Intent(this, MuteBroadcastReciever.class);

AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

PendingIntent pi = PendingIntent.getBroadcast(this, 747, i, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + (AlarmManager.INTERVAL_DAY * 7), pi);

But still it triggers before 7 days.

EJK
  • 12,332
  • 3
  • 38
  • 55
Roach
  • 610
  • 1
  • 8
  • 13

2 Answers2

0

From Android's AlarmManager page:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

Are you using AlarmManager.setExact(int type, long triggerAtMillis, PendingIntent operation) to make sure it is exact?

Nathan Bierema
  • 1,813
  • 2
  • 14
  • 24
  • 1
    This should probably be a comment, not an answer. If you are still seeking information from the OP (as your question-in-your-answer implies), then you have not yet identified the OPs problem. – EJK Apr 09 '16 at 07:09
0

Try following code it may be helpful for you

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR,7);
Intent intent1 = new Intent(this, MuteBroadcastReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    context, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context
                    .getSystemService(context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
  • I believe this repeats the alarm everyday. I don't intend to repeat the alarm. Just to shoot it after 7 days of the first install. – Roach Apr 09 '16 at 07:38
  • Ok...then you have require to cancel after complet your logic in reciver recive method – Dhaval Solanki Apr 09 '16 at 19:45