Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In android, how can i turn off the screen programatically?

Tags:

android

screen

I am trying to turn off my screen programatically.

The following methods i have tried:

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);

The previous mentioned method does not work.

This however, works:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

mDPM.lockNow();

This works, but i am turning my screen off while the phone is ringing, and this causes the ringer to stop ringing. The same as if you are pressing the power button.

Well, i need to be able to turn off my display WITHOUT using DevicePolicyManager method. Or a way to set it that DevicePolicyManager method won't stop my device from ringing.

Thanks all!

like image 257
Seth Avatar asked Sep 08 '13 01:09

Seth


1 Answers

its better to decrease the screen time out time to turn the screen off . like this :

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 10);

10 means 10 ms you can set your own !

like image 74
M410 Avatar answered Nov 14 '22 23:11

M410