多个火警在同一时间 [英] Multiple Alarm fire at same time

查看:158
本文介绍了多个火警在同一时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有触发在第一时间报警多的一个问题,这是我的code

i have a problem with triggering the multiple alarm at first time , here is my code

这就是我写下面的code ::发射活动

it is launcher activity where i write the following code ::

** onCreate方法:

**onCreate Method :

// Calling Method setNextAlarm two times..with different id and time 

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

和setNextAlarm在这里::

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

当我运行该code,它调用onBootReceiver这是我们的广播接收器类。

when i run this code it calls the onBootReceiver which is our Broadcast receiver class.

所以我的问题是:

限定在该方法的不同的时间和id之后

After defining the different time and id in this method

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

为什么火的同一时间?除第一次运行在固定的时间间隔罚款。

这是我onBootReceiver类的方法的onReceive

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

感谢。

推荐答案

在OnBootReceiver

in OnBootReceiver

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

下面是变化

我设置

SystemClock.elapsedRealtime() + 60000  for id = 0

和设置

SystemClock.elapsedRealtime() + 120000  for id = 1

和问题的解决。

但是我仍然在等待更好的解释。

我发现这里有关的资料很少 SystemClock.elapsedRealtime()

i found little information here about SystemClock.elapsedRealtime()

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

这篇关于多个火警在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆