Android: Turn screen on and off without locking

寵の児 提交于 2019-12-22 08:35:01

问题


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...:)


回答1:


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" />


来源:https://stackoverflow.com/questions/17344359/android-turn-screen-on-and-off-without-locking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!