如何关闭唤醒锁?
我尝试使用此代码在纵向时打开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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看起来每次旋转屏幕时都会创建一个新的唤醒锁,并且不会保留新的唤醒锁。
如果您查看日志,您可能会看到有关泄漏唤醒锁的信息。
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.