On Fri, Jun 10, 2011 at 11:42 AM, Matt Quigley
<[email protected]> wrote:
> We've got a pretty large app with millions of installs.  What we've
> noticed is that there are always ANRs reported, even in questionable
> conditions.  For example, we see ANRs in the most simple of broadcast
> receivers, where there are only 4 lines of code, and no networking or
> file IO.  The code takes 10ms to complete in normal conditions
> (although I've seen it as high as 60ms).

Have you used StrictMode to confirm that you are not using networking
or file I/O here? There may be file I/O under the covers in some
Android API calls you're using.

Also, bear in mind that referring to classes other than the
BroadcastReceiver itself may involve file I/O. We know that
unreferenced classes are not loaded into the Dalvik VM, which is why
we can conditionally load classes based on Build.VERSION.SDK_INT. What
isn't clear to me is whether the class is already in RAM when the
process started but just wasn't hooked into Dalvik, or whether the
class is loaded off of flash at the time we refer to it. Anybody know
what the rule is here?

> Now, BroadcastReceivers are supposed to have 10,000 milliseconds to
> complete (10 seconds).  And yet we _still_ see ANRs.  So, I'm curious,
> are we supposed to start a service for EVERY broadcast received?

Not that I'm aware of.

> For
> example:
>
>        @Override
>        public void onReceive(Context context, Intent intent)
>        {
>                Bundle extras = intent.getExtras();
>                if (extras != null)
>                {
>                        Intent serviceIntent = new Intent(ACTION);
>                        serviceIntent.setClass(context, TheService.class);
>                        serviceIntent.putExtras(extras);
>                        serviceIntent.putExtra(ORIGINAL_ACTION, 
> intent.getAction());
>                        context.startService(serviceIntent);
>                }
>        }

Intents are Parcelable, so I'd just use:

Intent serviceIntent=new Intent(context, TheService.class);
serviceIntent.putExtra(DA_BROADCAST, intent);
context.startService(serviceIntent);

But, again, that should only be necessary when the work to be done is
big enough to warrant it.

> On the other hand, what is the overhead of creating a new service for
> every broadcast received?  Let's say this broadcast is received 20
> times a day, will that be a lot of extra CPU usage in order to create
> new services, just to avoid the 1 out of 100,000 chance that there
> could be an ANR?  My initial tests showed that creating a service took
> about 100ms, although I admit I'd need to run the tests thousands of
> times over to get a better scientifically sound estimate.

Well, the process will already be set up, since your BroadcastReceiver
is running. 100ms seems plausible, though I haven't run those sorts of
tests yet either.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android 3.0 Programming Books: http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to