【问题标题】:Broadcast receiver onReceive() getting called multiple times广播接收器 onReceive() 被多次调用
【发布时间】:2011-09-20 01:23:17
【问题描述】:

我有一个 boot_completed 接收器,它会在启动时收到通知。

    <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 调试器连接到启动广播接收器或正在运行的服务,那将是非常棒的。

【问题讨论】:

  • 你有没有机会解决这个问题?

标签: android eclipse broadcastreceiver


【解决方案1】:

奇怪的是你会启动多个计时器。尝试将 PendingIntent.FLAG_ONE_SHOT 作为 PendingIntent.getBroadcast

中的最后一个参数传递

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多