Seth
Seth

Reputation: 1835

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

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!

Upvotes: 7

Views: 7418

Answers (1)

M410
M410

Reputation: 301

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 !

Upvotes: 1

Related Questions