开发者

Android, Does Wake Lock is active when In the hole of application?

开发者 https://www.devze.com 2023-04-04 17:39 出处:网络
I have some activities in my application. In main activity I have defined the wake lock: PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);

I have some activities in my application. In main activity I have defined the wake lock:

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");

and released it in onPause() method. My question is, is it active in the whole of application? or Do I need to copy/paste it i开发者_StackOverflow社区n each activity that I have?

Thanks


As long as WakeLock is acquired, so long wake lock is held. Related is lifetime of your WakeLock object. And if WakeLock is no more used and it is GC'd while still having lock, it's released during garbage collection (in finalize() method).

So your wake lock is active between acquire() and release(). You must make sure that your code calls these functions when needed from desired activities.

If you want WakeLock globally in your application, extend android.app.Application, specify it in manifest, and manage wake lock on application level.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号