繁体   English   中英

广播接收器onReceive()被多次调用

[英]Broadcast receiver onReceive() getting called multiple times

我有一个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调试器附加到Boot-up广播接收器或正在运行的服务上,那就太好了。

奇怪的是您要启动多个计时器。 尝试通过PendingIntent.FLAG_ONE_SHOT作为最后一个参数里面PendingIntent.getBroadcast

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM