广播接收器的onReceive()获取调用多次 [英] Broadcast receiver onReceive() getting called multiple times
问题描述
我有一个boot_completed接收器,大干快上的引导通知。
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");
}
作为一个方面说明,如果有人知道如何在Eclipse调试器附加到开机广播接收机或正在运行的服务,那将是非常美妙的。
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.
推荐答案
这是奇怪的是,你会得到多个定时器启动。尝试通过的 PendingIntent.FLAG_ONE_SHOT
作为内的最后一个参数的 PendingIntent.getBroadcast
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
这篇关于广播接收器的onReceive()获取调用多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!