Reputation: 23
Purpose :
in an android app, turn screen off when user touch a button.
Answers already exist :
Set screen brightness to zero.
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
This code makes screen dim, but can't turn off.
Use PowerManager.WakeLock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
//..screen will stay on during this section..
//wl.release();
This code keep a phone at 'Wake_State'. In following Table, 'PARTIAL_WAKE_LOCK' looks to turn off screen. But it just allows that a user turn screen off personally (maybe with power-button).
So, I want to turn screen fully off not using 'power-button'. Thanks for reading.
--- Add about my intention -------------------------
My user needs to turn screen off while other apps are keep working. Not to make 'POWER_KEY_DOWN(UP)_EVENT' I chose this way. I welcome another way which archive same purpose.
Upvotes: 2
Views: 3438
Reputation: 475
You can achieve it by setting the wake_setting time to 1 second.
I have already tried both of above shared ways, which shows slightly visible screen on but by setting wake_setting to 1 sec.
I have to just turn off the screen. For that purpose you are going to add SETTING_Change permission ( only available in rooted handset).
Upvotes: 2