WakeLock not working

前端 未结 2 1660
执念已碎
执念已碎 2021-01-20 17:28

I have a wake lock set up so that I can still hear the sound when the screen times out or I press the screen lock button. From what I can understand through reading online i

相关标签:
2条回答
  • 2021-01-20 17:58

    I would be surprised if Di Vero Labs's answer resolved this since Android documentation says those additional flags do nothing with a partial wakelock.

    A wakelock is as simple as you are using it, and it should work:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sleeplock");
    wl.acquire();
    //release
    wl.release(); 
    

    Note that you will need to check for nulls, and check that it is not already acquired, etc before doing these methods or you may get a null pointer exception.

    I think your issue is that you are calling cleanup() which includes the wakelock release so you aren't really holding a wakelock. To make sure this works (though might cause battery drain issues) hold your wakelock upfront, then release when you are done, not before you are done else you'll obviously NOT have a wakelock.

    I was having a ton of issues because wakelocks weren't working but believe I nailed down the issue to a custom ROM. So beware! (DONT support custom ROMs, including your own!)

    0 讨论(0)
  • 2021-01-20 18:06

    Try:

    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
                    PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.ON_AFTER_RELEASE, "sleeplock");
    

    I've found the "PowerManager.ACQUIRE_CAUSES_WAKEUP" alleviates a few of the same issues I was having.

    Also make sure:

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

    is declared in your manifest.

    0 讨论(0)
提交回复
热议问题