如果仅执行的AsyncTask广播接收器唤醒锁需要的?接收器、AsyncTask

2023-09-06 14:35:47 作者:滚开我要变身了

我有一个类,它广播接收器扩展,并得到从 AlarmManager 调用。在的onReceive 方法我执行的AsyncTask ,它获取从互联网上的一些数据,并在本地数据库中存储数据应用。

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();
}

为了从睡眠停止CPU或者是它的安全执行的AsyncTask 无唤醒锁?

推荐答案

我建议你阅读的文档的接收器的生命周期。你所要做的是不是好的做法,如果的onReceive()方法返回的过程中可以被杀死。因此,你的任务无法完成的工作。正因为如此,你应该启动一个服务/ IntentService运行任务,并保持这个过程活着。

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.

 
精彩推荐
图片推荐