Multiple alarms at the same time

I have a problem firing multiple alarm for the first time, here is my code

this is the start operation 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 setNextAlarm 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 onBootReceiver which is our class broadcast receiver.

So my question is:

After defining different time and id in this method

setNextAlarm(0,60000);
setNextAlarm(1,120000);

      

why does it fire at the same time? Except the first time it works fine at a fixed interval.

this is my onBeceive method onBootReceiver

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);

      

Thank.

+3


source to share


1 answer


in OnBootReceiver

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() + 60000, PERIOD, pi);

      

there is a change here

i install

SystemClock.elapsedRealtime() + 60000  for id = 0

      

and install



SystemClock.elapsedRealtime() + 120000  for id = 1

      

and the problem is solved.

But I'm still waiting for a better explanation.

i found some info here about SystemClock.elapsedRealtime ()

elapsedRealtime () is counted in milliseconds since system boot, including deep sleep. This clock should be used when measuring time intervals that can span periods of systemic sleep.

http://developer.android.com/reference/android/os/SystemClock.html

0


source







All Articles