2011-09-20 114 views
7

我有一个boot_completed接收器在启动时收到通知。广播接收器onReceive()多次调用

<receiver android:name=".BootCompletedReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

但它似乎被调用多次。我启动一个计时器,然后启动一个服务,这导致多个计时器,然后该服务被重置并再次运行。

创建像这样的计时器。这不是一个重复的计时器,是?:

 private void setAlarm(Context context, long interval) { 
     try { 
      AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      Intent intent = new Intent(RespondAlarmReceiver.ACTION_RESPOND_SMS); 
      intent.putExtra("isChecking", true); 
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

      int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; 
      long triggerAtTime = SystemClock.elapsedRealtime() + interval; //interval is 60,000 
      alarms.set(alarmType, triggerAtTime, alarmIntent); 
     } 
     catch (Exception e) { 
      Log.e(DEBUG_TAG, "Unable to set alarm"); 
     } 

作为一个侧面说明,如果有人知道如何在Eclipse调试器附加到开机广播接收器或正在运行的服务,这将是非常美妙的。

+0

内通过你解决了这个机会吗? –

回答

4

奇怪的是,你会得到多个定时器启动。尝试通过PendingIntent.FLAG_ONE_SHOT作为最后一个参数的PendingIntent.getBroadcast