如果 doWakefulWork 启动另一个服务怎么办?
我非常喜欢 CWAC Advanced Android 书中关于 AlarmManager 的部分。我现在担心我的应用程序的工作被终止,因为它没有唤醒锁。这就是我担心的事情。我的担心合理吗?
请参阅下面的代码。如果 doWakefulWork 必须启动另一个服务怎么办?在这种情况下,另一个服务需要获取 WakeLock 才能继续运行,但是服务启动和 onHandleIntent 结束之间有一个时间间隔,可以释放唤醒锁!
@Override
final protected void onHandleIntent(Intent intent) {
try {
doWakefulWork(intent)
}
finally {
getLock(this).release();
}
}
我的答案是: doWakefulWork 必须在同一线程上执行,它不能启动其他线程或服务,除非其他服务也是 WakefulIntentService 实例。
我说得对吗?
I really like the section on AlarmManager in the CWAC Advanced Android book. I am now worried about my app's work being killed because it doesn't have a wake lock. Here's what I am worried about. Is my worry rational?
See code below. What if doWakefulWork has to start another service? In this case, the other service needs to acuire a WakeLock to keep running, however the is a gap in time between the service starting and onHandleIntent ending where the wakelock could be released!
@Override
final protected void onHandleIntent(Intent intent) {
try {
doWakefulWork(intent)
}
finally {
getLock(this).release();
}
}
My going answer is:
doWakefulWork must execute on the same thread, it can't start other threads or services unless the other services are also WakefulIntentService instances.
Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望其他服务持有
WakeLock
,那么它们必须是WakefulIntentService
或自行获取WakeLock
。如果您担心发送意图和其他服务实际获取锁之间的时间,您将需要实现某种类型的阻塞机制(即阻止 doWakefulWork 方法结束,直到其他服务已启动并获取锁)If you want your other services to hold a
WakeLock
, then yes they must either be aWakefulIntentService
OR acquire theWakeLock
by themselves. If you are worried about the time between when you send the intent and when the other service actually acquires the lock you'll need to implement some type of blocking mechanism (ie block thedoWakefulWork
method from ending until the other service has started and aquired the lock)