Using BroadcastReceiver to Launch AsyncTask

I am trying to update the state of the user interface when receiving a push notification. To do this, I need to run AsyncTask, which performs some network operations, and then updates the user interface based on the result.

According to the documentation, BroadcastReceiverexecuting asynchronous operations at the receiver is unsafe because the process executing it can be killed as soon as it onReceive()returns, assuming there are no other “application components” in this process.

Does it work BroadcastReceiverin its own process or in the same process as containing activity? Since I only care about completing the task while the user interface is being updated, I don’t worry about death AsyncTaskif the activity is closed. Assuming BroadcastReceiver is in the same process as activity, does it make it safe / secure to run the task that I described in the receiver?

Edit:

To clarify, I register the recipient in action onResume()and do not register him onPause(), so he should only receive intentions when the activity is already active.

+5
source share
4 answers

, .

, onReceive , .

, , onReceive.

+5

AsyncTask, , , . , AsyncTask.

+1

Honeycomb (API11) .

Honeycomb (API11), goAsync():

onReceive (Context, Intent) . ( 10 ), , , - IO.

+1

, , startActivity(intent) . . , , . Activity onStart() onCreate(), . , Activity AsyncTask.

No need to use the service at all, with all the limitations of communication and communication from official activities.

Remember that you can also startActivityForResult(). I think you do not want to do anything other than the passage and forwarding inside the broadcast receiver.

By the way, for the activity you do not need to have a user interface. There may be faceless actions.

0
source

All Articles