Android,在应用程序中时唤醒锁是否处于活动状态?
我的申请中有一些活动。在主要活动中,我定义了唤醒锁:
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
并在 onPause() 方法中释放它。我的问题是,它在整个应用程序中是否活跃?或者我是否需要将其复制/粘贴到我进行的每项活动中?
谢谢
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 in each activity that I have?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要获取了 WakeLock,就可以保持很长时间的唤醒锁。相关的是 WakeLock 对象的生命周期。如果 WakeLock 不再被使用,并且它在仍然具有锁的情况下被 GC,它会在垃圾回收期间被释放(在 Finalize() 方法中)。
因此,您的唤醒锁在 acquire() 和 release() 之间处于活动状态。您必须确保您的代码在需要时从所需的活动中调用这些函数。
如果您希望在应用程序中全局使用 WakeLock,请扩展 android.app.Application,在清单中指定它,并在应用程序级别管理唤醒锁。
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.