0

我正在尝试关闭 android 屏幕,然后在几秒钟后再次打开。“关闭”部分有效,使用以下代码:

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);

但是,当我尝试再次打开屏幕时,它不适用于以下代码:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
layoutParam.screenBrightness = oldBrightness;
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

有什么想法可以解决吗?

谢谢

4

2 回答 2

1

我想你可以试试 Powermanager WakeLock 也许它会起作用。我在我的应用程序中使用此代码。它运作良好。:)

您还需要在清单中设置权限。

<uses-permission android:name="android.permission.WAKE_LOCK"/> // 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();
于 2014-08-25T07:18:09.173 回答
0

首先,您确定这一行中的“/255f”:

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

当您获取/设置它时,“oldBrightness”的值是多少?

也许你可以试试这个:

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

正常的唤醒锁不会打开屏幕,但会在用户启动时使其保持打开状态。但是这个标志会强制屏幕立即打开。它需要“android.permission.WAKE_LOCK”。

更多关于它:

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

以及屏幕属性(开、关、亮、暗等):

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

于 2012-05-06T12:55:02.090 回答