如何关闭唤醒锁?

发布于 2025-01-06 04:43:59 字数 1401 浏览 2 评论 0原文

我尝试使用此代码在纵向时打开proximitywakelock,在横向时关闭它,但它始终保持活动状态:

//in onCreate() of my Activity

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mProximityWakeLock = pm.newWakeLock(32, ""); // proximity_wake_lock=32
    mProximityWakeLock.setReferenceCounted(false);

    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "non landascape", Toast.LENGTH_SHORT).show();

        if (!mProximityWakeLock.isHeld()) {
            mProximityWakeLock.acquire();
            Toast.makeText(this, "acquired", Toast.LENGTH_SHORT).show();
        }

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

    } else {

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

        Toast.makeText(this, "landascape", Toast.LENGTH_SHORT).show();

        if (mProximityWakeLock.isHeld()) {
            mProximityWakeLock.release();
            Toast.makeText(this, "released", Toast.LENGTH_SHORT).show();
        }

    }

看起来在横向时它从未被保持...... isHeld() 实际上是如何工作的? 代码有什么问题?

I'm tryng to turn proximitywakelock on when portrait and off when landscape using this code, but it always stays active:

//in onCreate() of my Activity

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mProximityWakeLock = pm.newWakeLock(32, ""); // proximity_wake_lock=32
    mProximityWakeLock.setReferenceCounted(false);

    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "non landascape", Toast.LENGTH_SHORT).show();

        if (!mProximityWakeLock.isHeld()) {
            mProximityWakeLock.acquire();
            Toast.makeText(this, "acquired", Toast.LENGTH_SHORT).show();
        }

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

    } else {

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

        Toast.makeText(this, "landascape", Toast.LENGTH_SHORT).show();

        if (mProximityWakeLock.isHeld()) {
            mProximityWakeLock.release();
            Toast.makeText(this, "released", Toast.LENGTH_SHORT).show();
        }

    }

It looks like it's never held when on landscape...
How does isHeld() actually works?
What's the problem with the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

贱贱哒 2025-01-13 04:43:59

我看起来每次旋转屏幕时都会创建一个新的唤醒锁,并且不会保留新的唤醒锁。

如果您查看日志,您可能会看到有关泄漏唤醒锁的信息。

I looks like you are creating a new wakelock every time you rotate the screen and the new wakelock is not held.

If you look in the logs you might see something about leaking wakelocks.

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