Aziz
Aziz

Reputation: 479

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?

Upvotes: 2

Views: 2495

Answers (3)

dalton
dalton

Reputation: 81

If using the ELAPSED_REALTIME clock, do this

manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime()+180000,
                180000,
                pendingIntent);

Do not use zero or System.currentTimeMillis() as an offset for the ELAPSED clock.

Upvotes: 0

Ravi Bhatt
Ravi Bhatt

Reputation: 1948

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. :)

Upvotes: 6

noxius
noxius

Reputation: 144

check into handler.postDelayed function where you can specify a delay

Upvotes: 0

Related Questions