i have a problem with triggering the multiple alarm at first time , here is my code
it is launcher activity where i write the following code ::
**onCreate Method :
// Calling Method setNextAlarm two times..with different id and time
setNextAlarm(0,60000);
setNextAlarm(1,120000);
and the setNextAlarm is here ::
private void setNextAlarm(int id,long time) {
AlarmManager mgr = (AlarmManager) TestAct.this
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(TestAct.this, OnBootReceiver.class);
i.putExtra("id", id);
Log.e("Firing up next alarm in "+id,""+time/(60*1000) +"minutes");
PendingIntent pi = PendingIntent.getBroadcast(TestAct.this, id, i,PendingIntent.FLAG_ONE_SHOT);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
}
when i run this code it calls the onBootReceiver which is our Broadcast receiver class.
So My question is ::
After defining the different time and id in this method
setNextAlarm(0,60000);
setNextAlarm(1,120000);
why it fires at same time ? Except the first time it runs fine at fixed interval.
this is my onBootReceiver class’s onReceive method
Bundle b = intent.getExtras();
int id = b.getInt("id");
if(id==1){
PERIOD=300000;
}else{
PERIOD=120000;
}
Log.e("OnBootReceiver"," Calling"+id+" in "+PERIOD/60000 + "minutes");
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
i.putExtra("id", id);
PendingIntent pi=PendingIntent.getBroadcast(context, id,i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,
PERIOD,
pi);
Thanks.
in OnBootReceiver
here is change
i set
and set the
and the problem resolved.
But i am still waiting for Better Explanation.
i found little information here about SystemClock.elapsedRealtime()
elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep.
http://developer.android.com/reference/android/os/SystemClock.html