gpt4 book ai didi

java - 如果仅执行 AsyncTask,是否需要 BroadcastReceiver 唤醒锁?

转载 作者:行者123 更新时间:2023-11-29 05:19:46 26 4
gpt4 key购买 nike

我有一个类,它从 BroadcastReceiver 扩展并从 AlarmManager 调用。在 onReceive 方法中,我执行一个 AsyncTask,它从 Internet 获取一些数据并将数据存储在应用程序的本地数据库中。

我是否需要通过以下方式获取唤醒锁:

@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 hibernate 或者在没有唤醒锁的情况下执行 AsyncTask 是否安全?

最佳答案

OnReceive 方法 reference来自 BroadcastReceiver 文档说:

..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)

所以你最好用 service 替换你的 AsyncTask然后从 OnReceive 方法调用此服务。即使 broadcastReceiver 被杀死,您的服务也会继续运行。

关于java - 如果仅执行 AsyncTask,是否需要 BroadcastReceiver 唤醒锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25229685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com