Broadcast receiver onReceive() getting called multiple times

前提是你 提交于 2019-12-23 07:53:05

问题


I have a boot_completed receiver which gets notified on boot.

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

But it appears to get called multiple times. I start a timer, and then a service, which leads to multiple timers, and then the service gets reset and runs again.

Creating timer like this. This is not a repeating timer, is it?:

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

As a side note, if anybody knows how to attach the Eclipse debugger to the Boot-up broadcast receiver or to a running service, that would be fantastic.


回答1:


It's strange that you'd be getting multiple timers started. Try passing PendingIntent.FLAG_ONE_SHOT as the last argument inside of PendingIntent.getBroadcast



来源:https://stackoverflow.com/questions/7479101/broadcast-receiver-onreceive-getting-called-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!