android on off控件,Turn off/on android screen

问题

I am trying to turn the android screen off and then on again, after a few seconds. The "turn off" part works, with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();

oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;

layoutParam.screenBrightness = 0;

layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

getWindow().setAttributes(layoutParam);

But then, when I try to turn the screen on again, it does not work with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();

layoutParam.screenBrightness = oldBrightness;

layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

getWindow().setAttributes(layoutParam);

Any idea to solve that ?

thanks

回答1:

I think U can try Powermanager WakeLock maybe it will work. I m using this code in my application. and it works well. :)

Also u need to set permission in manifest.

// Manifest Permission

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK

| PowerManager.ACQUIRE_CAUSES_WAKEUP

| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");

wakeLock.acquire();

回答2:

First off, are you sure about the "/255f" in this line:

oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;

What is the value in "oldBrightness" when you get/set it ?

Maybe you could try this:

PowerManager.WakeLock lck = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");

lck.acquire();

The normal wake lock doesn't turn the screen on but causes it to stay on when a user causes it. But this flag forces the screen to turn on immediately. It requires the "android.permission.WAKE_LOCK".

More about it:

http://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK

And the screen properties (on, off, bright, dim, etc.):

http://developer.android.com/reference/android/os/PowerManager.html

来源:https://stackoverflow.com/questions/10469511/turn-off-on-android-screen

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值