2

我尝试了几种已发布的解决方案来尝试在 android 中关闭屏幕,包括:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

这导致没有变化。

WindowManager.LayoutParams param = getWindow().getAttributes();
param.screenBrightness = 0;
param.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(param);

这导致屏幕略微变暗(在 Android 4.2.1 上)

//Release all previously held WakeLocks
if (mWakeLock.isHeld()) {
    mWakeLock.release();
}
//Then acquire a partial wake lock (which should allow the display to turn off)
PowerManager powerMan = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = powerMan.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Partial WakeLock");
mWakeLock.acquire();

这导致没有变化。

最后,非法

PowerManager powerMan = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
powerMan.goToSleep(1000);

这显然不起作用,因为:

<uses-permission android:name="android.permission.DEVICE_POWER" />

是不允许的(对于非系统应用程序)。

我假设这些问题是因为我的应用程序是android:theme="@style/FullscreenTheme".

是否有任何可行的解决方案可以让全屏应用程序在不牺牲精彩的全屏主题的情况下进入睡眠状态?

4

1 回答 1

0

After writing a simple answer to try and post to @CommonsWare, I found that the problem didn't persist.

I combed through my code several times, and still didn't find anything which would cause the screen to stay awake. I rewrote the entire app from scratch (always a good exercise, ending up with much cleaner logic), and the problem disappeared.

Lesson learnt - if all else fails, re-write everything again. It's a useful exercise in itself :)

于 2013-01-28T02:26:39.620 回答