Android的Java的:打开屏幕关闭 [英] Android Java : Turn screen Off
问题描述
我想提出的ON和OFF打开屏幕,接近传感器的应用程序。 接近code结束,但我使用的屏幕控制了麻烦。
I am making an application that turn the screen ON and OFF with the proximity sensor. The proximity code is finished, but i got trouble using the screen controls.
我看过,我应该使用,
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(int amountOfTime);
对于这一点,我需要授予为了让它工作的特殊权限,但我还没有想出如何做到这一点。
For that, I need to grant special permissions in order to make it works, but I haven't figured out how to do it.
另外,我看了一下改变屏幕亮度
Also, I have read about changing screen brightness
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
不过,这种方式只关闭屏幕上的我的应用程序;如果我的应用程序在后台运行,这是行不通的。
But this way only turn the screen off on my application; it doesn't work if my application is running in background.
我也看到了有关使用Wakelock(我用它们来唤醒我的手机从屏幕关闭),但是当
I have also read about using Wakelock (i use them to wake my phone from screen-off), but when
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();
但是,当我做到这一点,没有任何反应。
But when I do that, nothing happens.
有没有其他办法做到这一点?
Is there any other way to do it ?
推荐答案
您需要给你的应用程序正确的权限做到这一点:
You need to give your App the right permissions to do that:
添加<使用-权限的Android:名称=android.permission.DEVICE_POWER/>
你的&LT内部清单,清单&GT ;
标签
如果你想保持你的屏幕上使用这个建议的那样,戴安娜Hackborn在谷歌加: KeepScreenOn
If you want to keep your Screen on use this, as suggested by Dianne Hackborn on Google Plus: KeepScreenOn
这篇关于Android的Java的:打开屏幕关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!