android's alarm manager
Hello I have a question about android's alarm manager, I would like to schedule an alarm that would execute using an initial interval and be able to change by the user. my code is :
long firstTime = SystemClock.elapsedRealtime();
mAlarmSender = PendingIntent.getService(mContext, 0, new Intent(mContext,AlarmService_Service.class),开发者_运维问答 0);
this.setInterval(Integer.parseInt(Interval));
configShared.edit().putInt("interval", this.getInterval()).commit();
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,this.getInterval(), mAlarmSender);
the this.getInterval() is a method to hold the interval and is used to check if user has changed the interval.
Reading the documentation I found under the parameters of alarmManagers setRepeating method that says:
triggerAtTime Time the alarm should first go off, using the appropriate clock (depending on the alarm type)
What exactly is this? Is the alarm canceled when I change the interval? also there's a problem cause I like this code to be executed every 5 minutes and this will execute every 5 minutes+firstinterval correct?
What excactly is this?
It seems fairly self-explanatory. It is the time when the alarm will first go off. Subsequent alarms will go off at your specified period after that time.
Is the alarm canceled when I change the interval?
There is no such thing as "change the interval". You can set alarms and cancel alarms. You cannot update alarms, except by canceling the old and adding the new.
also there's a problem cause I like this code to be executed every 5 minutes and this will execute every 5 minutes+firstinterval correct?
The first one will occur at firstTime
. The second one will occur at firstTime
plus five minutes. The third one will occur at firstTime
plus ten minutes. And so on. Since firstTime
is now, the first alarm should go off immediately.
精彩评论