简体   繁体   English

Android:无需锁定即可打开和关闭屏幕

[英]Android: Turn screen on and off without locking

I need to turn the screen off and on like when you are doing a call and holding the phone to your ear. 我需要关闭和打开屏幕,就像打电话并将手机放在耳边一样。 The screen then turns off but it isn't locked or anything, it's just black. 屏幕然后关闭,但它没有锁定或任何东西,它只是黑色。

What is the best way to do that? 最好的方法是什么? I don't want to show a black fullscreen activity if there is another solution...:) 如果还有其他解决方案,我不想显示黑色全屏活动... :)

There are two methods for doing that, 有两种方法可以做到这一点,

    PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);

// method 1
manager.goToSleep(int amountOfTime);

// method 2
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();// It will swith off
wl.release(); // And swith on

also add this permission, 还添加此权限,

<uses-permission android:name="android.permission.WAKE_LOCK" />

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

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