Android - 唤醒锁线程未打开屏幕
我在唤醒锁方面遇到了麻烦。基本上,我在我的计时器线程中运行唤醒锁,这是在我的应用程序的整个持续时间内 AsyncTask 的 doInBackground (它是用于进行性能测量的后台应用程序)。然后我决定只希望屏幕每 10 分钟左右唤醒一秒钟左右。因此,我创建了另一个扩展 AsyncTask 的类,并将下面的代码放入它的 doInBackground 中,但现在屏幕无法重新打开。我应该注意,我启动了这个线程和另外两个线程,它们是使用 onCreate 中的 doInBackground 方法的 AsyncTask 线程。
这是我的新内部类执行唤醒操作:本质上它应该做的就是每 10 分钟唤醒手机屏幕一段时间,直到我的其他两个后台线程将其布尔值设置为 true。
private class WakeUp extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
do{
try {
Thread.sleep(WAKEUP_EVERY); //600000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
wl.acquire();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wl.release();
}while(!timeCompleted || !transferCompleted);
return null;
}
}
I am having trouble with wakelocks. Basically, I had the wakelock running in my timer thread, a doInBackground of an AsyncTask for the entire duration of my app (it is a background app for taking performance measurements). Then I decided I only want the screen to wakeup every 10 minutes or so for a second or so. So I created another class extending AsyncTask and put the code below into it's doInBackground, but now the screen doesn't turn back on. I should note that I start this thread and two other threads that are AsyncTask with doInBackground methods from onCreate.
Here is my new inner class doing the waking up: Essentially all it is supposed to do is wake the phone screen up every 10 minutes for a bit until my other two background threads set their booleans true.
private class WakeUp extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
do{
try {
Thread.sleep(WAKEUP_EVERY); //600000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
wl.acquire();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wl.release();
}while(!timeCompleted || !transferCompleted);
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记告诉唤醒锁使用 ACQUIRE_CAUSES_WAKEUP 标志打开屏幕。根据文档:
有关更多详细信息,请参阅 ACQUIRE_CAUSES_WAKEUP :D
You've forgotten to tell the wake lock to turn on the screen using the ACQUIRE_CAUSES_WAKEUP flag. As per the documentation:
See ACQUIRE_CAUSES_WAKEUP for more details :D