Aziz Aziz - 6 months ago 43
Android Question

How to delay Trigger(starting time) of repeating alarm?

The problem is that I want to delay the repeating-alarm's first fire.
for example I want this pending intent work after 10 mins of the click not right away, how to do that?

public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Main.this, ReportService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar c = Calendar.getInstance();
PendingIntent pendingIntent = PendingIntent.getService(Main.this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 112500, pendingIntent);
}


the 2nd paramater in this setInexactRepeating called triggerAtMillis
From Android developer guide:
triggerAtMillis time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type). This is inexact: the alarm will not fire before this time, but there may be a delay of almost an entire alarm interval before the first invocation of the alarm.

Iam changing it and it always start on the click and does no delay, any help?

Answer

Check the developer link here. The second parameter is triggerAtMillis. It used as time in milliseconds that the alarm should first go off

So in your case, add the extra milliseconds to c.getTimeInMillis(), after which you want to first run your pending intent. :)