简体   繁体   English

Android Java:关闭屏幕

[英]Android Java : Turn screen Off

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. 接近代码已完成,但我在使用屏幕控制时遇到了麻烦。

I have read that I should use, 我读过我应该用的,

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. 如果我的应用程序在后台运行,它不起作用。

I have also read about using Wakelock (i use them to wake my phone from screen-off), but when 我还读过关于使用Wakelock的信息(我用它来唤醒我的手机屏幕关闭),但是什么时候

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: 您需要为您的应用程序提供正确的权限:

Add <uses-permission android:name="android.permission.DEVICE_POWER" /> to your Manifest inside the <manifest> Tag <uses-permission android:name="android.permission.DEVICE_POWER" /><manifest>标签内的Manifest中

If you want to keep your Screen on use this, as suggested by Dianne Hackborn on Google Plus: KeepScreenOn 如果您希望屏幕保持使用,请按照Dianne Hackborn在Google Plus上的建议: KeepScreenOn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM