Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver onReceive timeout

I extend BroadcastReceiver, and in onReceive() I do whatever I need to do.

onReceive() has a timeout, from the documentation:

there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed

This creates a problem when I am in debug mode. I need more than 10 seconds (sometimes). If I don't do all my debugging in 10 seconds my connection is closed and debugging is stopped.

Can I increase the timeout or disable it for debugging purposes?

Thanks.

like image 993
Alexandru Luchian Avatar asked Jan 10 '11 18:01

Alexandru Luchian


People also ask

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

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

What is the life cycle of BroadcastReceiver?

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent) . Once your code returns from this function, the system considers the object to be finished and no longer active.

What is the time limit of broadcast receiver in Android?

There is also a 5-10 second limit, after which Android will basically crash your app. However, you cannot reliably fork a background thread from onReceive() , as once onReceive() returns, your process might be terminated, if you are not in the foreground.

Is it necessary to unregister broadcast receiver?

Be mindful of where you register and unregister the receiver, for example, if you register a receiver in onCreate(Bundle) using the activity's context, you should unregister it in onDestroy() to prevent leaking the receiver out of the activity context.


2 Answers

In order to prevent your app from force closing while you are paused on a break point during debugging, try installing the Dev Tools application and enable the Debug App setting which:

Lets you select the application to debug. You do not need to set this to attach a debugger, but setting this value has two effects:

  • It will prevent Android from throwing an error if you pause on a breakpoint for a long time while debugging.

All of the details are here: http://developer.android.com/tools/debugging/debugging-devtools.html

If you are doing something complicated in your onReceive method, then consider having your BroadcastReceiver start a Service and pass along the data it gets from within onReceive. The Service can then do the longer processing.

like image 173
Eric Levine Avatar answered Sep 20 '22 08:09

Eric Levine


I encountered this problem even when in debug mode. It turns out that another broadcast was being sent and wasn't being handled because I was debugging the other broadcast on the main thread. Android considered my process to be in ANR and killed the whole process.

I had to temporarily modify the code to not call the other broadcast while doing my debugging.

like image 24
Jeremy Stein Avatar answered Sep 20 '22 08:09

Jeremy Stein