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

ami*_*mit 8 eclipse android broadcastreceiver

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

    <receiver android:name=".BootCompletedReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
    </receiver>
Run Code Online (Sandbox Code Playgroud)

但似乎多次被召唤.我启动一个计时器,然后一个服务,它导致多个计时器,然后服务重置并再次运行.

像这样创建计时器.这不是重复计时器,是吗?:

     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");
        }
Run Code Online (Sandbox Code Playgroud)

作为旁注,如果有人知道如何将Eclipse调试器连接到启动广播接收器或正在运行的服务,那将是非常棒的.

Zai*_*ani 4

奇怪的是你会启动多个计时器。尝试PendingIntent.FLAG_ONE_SHOT作为最后一个参数传递PendingIntent.getBroadcast