Asynctask inside BroadCastReceiver
I want to use an As开发者_如何学Cynctask inside BroadCastReceiver so that I can show caller information when I receive a call.
Can someone tell me how to do this.
Regards,
Shankar
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.
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.
精彩评论