I'm trying to turn off the screen of an Android phone.
I read many other discussions, and tried two ways discribed here: Turn off screen on Android
In fact, what I really need to do is to launch the lock screen (the one that asks for the PIN). I wasn't sure how to do that and couldn't find documentation about it, so I tried to turn off the screen and hoped that it will display the lock screen when I turn the phone back on. Unfortunately, I wasn't able to test my theory.
First option:
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
wl.acquire();
wl.release();
Second option:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
First option did nothing, and the second option caused my app to crash. In the stack call, I saw a suspicious log:
Only the original thread that created a view hierarchy can touch its views.
I do call this code from another thread, running in the background (not the main thread).
Is it possible that this is causing the crash? How can I overcome this problem?
Is there another way to programmatically launch the lock screen, without having to turn off the screen?