Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver onReceive() getting called multiple times

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.

like image 676
amit Avatar asked Sep 20 '11 01:09

amit


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

Does the broadcast receiver onReceive () runs on main thread or background thread?

Because a receiver's onReceive(Context, Intent) method runs on the main thread, it should execute and return quickly. If you need to perform long running work, be careful about spawning threads or starting background services because the system can kill the entire process after onReceive() returns.

What is broadcast receiver in an Android app?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is the difference between service and BroadcastReceiver in Android?

A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.


1 Answers

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

like image 73
Zaid Daghestani Avatar answered Oct 08 '22 05:10

Zaid Daghestani