1

I have got a class, which extends from BroadcastReceiver and gets called from AlarmManager. In the onReceive method I execute an AsyncTask, which fetches some data from the internet and stores the data in the local database of the application.

Do I need to acquire wakelock with:

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    mWakeLock.acquire();

    // execute AsyncTask
}

private void asyncTaskDone() {
    mWakeLock.release();
}

in order to stop the CPU from sleeping or is it safe to execute the AsyncTask without a wake lock?

Niklas
  • 23,674
  • 33
  • 131
  • 170

2 Answers2

2

The OnReceive method reference from the BroadcastReceiver doc says :

..you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed)

So you'd better replace your AsyncTask with a service and then call this service from the OnReceive method. Your service will keep running even if the broadcastReceiver get killed.

sosta
  • 102
  • 3
1

I suggest you to read the docs about the receiver lifecycle. What you are trying to do is not good practice, If the onReceive() method returns the process can be killed. Hence your task can't complete the work. Because of this you should start a Service/IntentService to run the task and to keep the process alive.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • 1
    Agreed. Moreover, `onReceive()` is called on the main application thread, and so an answer of "I will do the work in `onReceive()` itself will not work either for anything as long as you are describing. With respect to the `WakeLock`, [use `WakefulBroadcastReceiver`](http://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html) or [my `WakefulIntentService`](https://github.com/commonsguy/cwac-wakeful). – CommonsWare Aug 10 '14 at 15:13
  • So I have to create a Service, which I will call in my `BroadcastReceiver` and in that Service class I will execute my `AsyncTask`? – Niklas Aug 10 '14 at 15:22
  • @Niklas Yes this way you prevent that the task could be killed along with the process of your app in some circumstances. It's a bit more code in turn of a safe workflow. – Steve Benett Aug 10 '14 at 15:26
  • @SteveBenett since I start my `BroadcastReceiver` currently with an `AlarmManager`, isn't it possible to start the `Service` directly and remove the `BroadcastReceiver`? – Niklas Aug 10 '14 at 15:28
  • @Niklas Sure, if you don't need the Receiver to be called as an entry point to your app f.e. this would be the better approach. – Steve Benett Aug 10 '14 at 15:31
  • @SteveBenett one last question, do I need wake lock in my Service class? – Niklas Aug 10 '14 at 15:38
  • 1
    @Niklas Depends, in some situations it is needed. [Here's](https://developer.android.com/training/scheduling/wakelock.html#cpu) a nice guide at which time you should use wake locks. – Steve Benett Aug 10 '14 at 16:03