简体   繁体   中英

PARTIAL_WAKE_LOCK does not work

I created a new project with target API 15 (ICS) with empty Activity. I added permission to manifest:

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

I added code to onCreate() :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PowerManager pm = (PowerManager) getSystemService(getApplicationContext().POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
        wl.acquire();

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

But nothing happens with the device. According to PowerManager documentation I expected the screen to go off (immediately). So, am I doing something wrong or is this not working?


EDIT:

I tried Ashish Ranjan's suggestion to manually set screenBrightness after acquiring WakeLock but this too does not work.

As per the Android documentation , using PARTIAL_WAKE_LOCK won't turn the screen off. But it will allow the screen to go off, when this mode is active in the WakeLock .

So, the device screen won't turn off instantly, you'll have to wait for the screen to timeout (which depends on the time set in the device display settings) but the CPU will keep running.

PARTIAL_WAKE_LOCK

Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released.

To turn off the screen you'll have to change the Window LayoutParams like this :

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

我认为您忘了添加:

 wl.release();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM