WakeLock not working

♀尐吖头ヾ 提交于 2019-12-01 21:26:42

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!