Search code examples
androidandroid-intentserviceandroid-widgetappwidgetprovider

Huge delay between sending and receiving an intent


EDIT:

The problem that I explained here had nothing to do with intents. Instead, rotating a bitmap took 300ms with the method I chose. Now I found a way of rotating within 10 - 15ms, so the problem is solved.


Some context to my problem:

  • I have a compass widget and a service which gets started when I press a button on the widget.
  • This service asks android for GPS and sensor data and sends them to the widget via intent for a few minutes.
  • The AppWidgetProvider then spins the compass needle and does other stuff.

Now the problem:

  • When I press that button, the service starts sending intents right away and the widget deals with the incoming data with one or two seconds delay.

After the service is running for some time, it slowly goes back to normal, which I find kind of weird ...

I think this is best described by just posting the two parts of the log next to each other:

WidgetIntentService: onCreate() called.
WidgetIntentService: Service onStartCommand
WidgetIntentService: The service has started.
WidgetIntentService: Updates started.
WidgetIntentService: onDestroy() called.
WidgetIntentService: Heading: 330.0 degrees
WidgetIntentService: Heading: 338.0 degrees
WidgetIntentService: Heading: 342.0 degrees
WidgetIntentService: Heading: 340.0 degrees
WidgetIntentService: Heading: 334.0 degrees
WidgetIntentService: Heading: 329.0 degrees
WidgetIntentService: Heading: 331.0 degrees
WidgetProvider1: Rotation: 330.0
WidgetIntentService: Heading: 330.0 degrees
WidgetProvider1: Rotation: 338.0
WidgetIntentService: Heading: 327.0 degrees
WidgetProvider1: Rotation: 342.0
WidgetIntentService: Heading: 326.0 degrees
WidgetProvider1: Rotation: 340.0
WidgetIntentService: Heading: 322.0 degrees
WidgetProvider1: Rotation: 334.0
WidgetIntentService: Heading: 316.0 degrees

...

WidgetIntentService: Heading: 334.0 degrees
WidgetProvider1: Rotation: 334.0
WidgetIntentService: Heading: 335.0 degrees
WidgetProvider1: Rotation: 335.0
WidgetIntentService: Heading: 335.0 degrees
WidgetProvider1: Rotation: 335.0
WidgetIntentService: Heading: 336.0 degrees
WidgetProvider1: Rotation: 336.0

Sometimes this delay doesn't go away until I start the service the second time. This is what the log looks like in this case:


...

WidgetIntentService: GPS stopped.
WidgetIntentService: Magnetometer stopped.
WidgetProvider1: Rotation: 293.0
WidgetProvider1: Rotation: 293.0
WidgetProvider1: Rotation: 294.0
WidgetProvider1: Rotation: 296.0
WidgetProvider1: Rotation: 299.0

My theory is, that android buffers the intents and that all these intents get worked off one at a time until the widget manages to keep up.

Is there any way that I can tell my AppWidgetProvider to only take the last intent received and ignore all the others?

If you have any idea of how I could do this in a better way, please let me know!


Solution

  • That's what an IntentService does- it queues up requests and handles them one at a time on a single non-UI thread. If you don't want that behavior, don't use IntentService. Use service and handle any threading yourself.