简体   繁体   中英

Unable to Keep Screen from Sleeping in Android 4.2

I have a simple battery test app I wrote for Android and have used for several versions now successfully. It works fine in Android 4.1 The app simply launches the chrome browser to a different web site, logs the time to a database, waits 60 seconds and does it all over again.

Now, in Android 4.2, the screen turns off and system goes to sleep despite the fact that I have a wakelock and have set screen timeout to never using settings. What's most annoying is it turns off at an indeterminate time, not a fixed time after starting the test. Any idea what I need to do?

Here's the wakelock code I call in the service that launches the browser over and over again.

    super.onCreate();
    // Wakelock
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
    wl.acquire();

I also put this in the layout file

android:keepScreenOn="true"

However, the app's screen itself isn't visible throughout the test as Chrome is always the foreground window and the browser launcher is a service and the wakelock is in the service.

Any idea how to keep the screen from timing out? Nothing seems to work.

Just add following line to your XML file......

android:keepScreenOn="true"

:)

I would guess that Android is killing your service and causing your wakelock to be released. Try making your service run in the foreground (with startForeground() ) and see if that helps.

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