1

I want to use an Asynctask inside BroadCastReceiver so that I can show caller information when I receive a call.

Can someone tell me how to do this.

Regards,

Shankar

Bhabani Shankar
  • 1,267
  • 4
  • 22
  • 41

2 Answers2

4

You shouldn't use background tasks with broadcast receivers. Broadcast receiver component is considered destroyed as soon as it returns from its onReceive() function. And if this was the only component in process, the process can get killed at any time.

If you need to run some background task as reaction to received broadcast, start a service and run background task as part of Service component.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Actully I do not want to process anything in the background. I want to show some text in the screen while phone is ringing. Anything u can suggest ? – Bhabani Shankar Aug 22 '11 at 10:30
1

Before Honeycomb (API11), you had to use a service.

Since Honeycomb (API11), you can use goAsync() :

This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function. This does not change the expectation of being relatively responsive to the broadcast (finishing it within 10s), but does allow the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.

Also, in order to show somethign while the phone is ringing, you will have to create a foreground service that keeps an on-top view alive , as shown here.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270