Android - 唤醒锁线程未打开屏幕

发布于 2024-10-18 07:04:54 字数 1187 浏览 3 评论 0原文

我在唤醒锁方面遇到了麻烦。基本上,我在我的计时器线程中运行唤醒锁,这是在我的应用程序的整个持续时间内 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2024-10-25 07:04:54

您忘记告诉唤醒锁使用 ACQUIRE_CAUSES_WAKEUP 标志打开屏幕。根据文档:

正常的唤醒锁实际上不会打开照明。相反,它们会导致照明在打开后保持打开状态(例如,由于用户活动)。当获取 WakeLock 时,此标志将强制屏幕和/或键盘立即打开。典型用途是对于用户立即看到很重要的通知。

有关更多详细信息,请参阅 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:

Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

See ACQUIRE_CAUSES_WAKEUP for more details :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文