很奇怪 - Android IntentService杀死了我的应用程序的其余部分,但一直在运行

时间:2014-04-24 10:10:09

标签: android android-intent service crash intentservice

我在Services / IntentServices中遇到了这个棘手的Android问题......

我有一个服务,它启动一个管理员类,要求从Google Play服务更新ActivityRecognition(即获取连接,然后在ActivityRecognitionClient上调用requestActivityUpdates()并传递PendingIntent)。

PendingIntent引用了一个实现了onHandleIntent(Intent)的IntentService - 它只是将最可能的当前物理活动打印到Log。

到目前为止一切顺利。一切正常 - 服务绑定,Manager连接,触发IntentService并将用户体育活动写入Log。

这是问题......

我希望这只是坐在后台并监听活动更新,即使我的用户关闭了“最近的应用”屏幕中的UI活动。我已在我的应用程序中使用此技术进行位置更新 - 即使应用程序的UI部分被用户或系统销毁,我的位置服务也会保持跟踪位置。

事实上,由于添加了这个新的Activity识别内容,当用户杀死UI时,IntentService接收的下一个PendingIntent会终止,其余的所有App都会包含所有其他服务。一切都停止/死亡/消失。但是没有警告,没有例外,没有日志条目,没有任何东西!

唯一能继续工作的是该死的IntentService!每当新的Intent进来时,这种情况就会发生变化。

如果我注释掉我注册活动更新的代码(因此没有使用PendingIntent并且没有调用IntentService),一切正常。

我不知道是什么原因造成了这场灾难,没有任何线索可以在日志中消失。

我搜索并搜索了Google / SO,但我画了一个空白。我找不到任何描述类似行为的人。

那么是否有其他类型的Android组件能够被Intent触发,我可以尝试而不是IntentService? IntentService真的是问题吗?活动识别不能像这样在后台完成吗?我还可以尝试其他什么?

更新:当IntentService上的onHandleIntent()方法完成时,操作系统可能会杀死我应用中的所有线程,而不仅仅是为IntentService创建的线程吗?

以下是日志中的内容......

--User is shutting down the App UI, but the Services are left running as intended...
19:41:12.146  13679-13679/tripcomputer I/tripcomputer.TripComputer? STOPPED TripComputer Activity: 1107344720
19:41:12.153  1258-4401/? W/ContextImpl? Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1244 android.content.ContextWrapper.sendBroadcast:365 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.PauseResumeTrigger.handleFrameworkEvent:53 com.motorola.motocare.internal.frameworkevents.FwEventMonitor$FrameworkListener.processFrameworkEvent:114
19:41:12.645  13679-13679/tripcomputer I/tripcomputer.TripComputer? DESTROYING TripComputer Activity: 1107344720
19:41:12.646  13679-13679/tripcomputer I/tripcomputer.services.JourneyServiceConnectionManager? Activity 1107344720 is STOPPING the JourneyService...
19:41:12.652  13679-13679/tripcomputer I/tripcomputer.services.JourneyServiceConnectionManager? IGNORING request to STOP the JourneyService - Journey in progress.
19:41:12.655  13679-13679/tripcomputer I/tripcomputer.services.ActivityServiceConnectionManager? Activity 1107344720 is STOPPING the ActivityService...
19:41:12.657  13679-13679/tripcomputer I/tripcomputer.services.ActivityServiceConnectionManager? IGNORING request to STOP the ActivityService - Activity in progress.
19:41:12.659  13679-13679/tripcomputer I/tripcomputer.TripComputer? DESTROYED TripComputer Activity: 1107344720

--The UI has closed down sucessfully. 
--The next activity update arrives at the IntentService and gets printed...
19:41:19.703  13679-14095/tripcomputer I/tripcomputer.services.ActivityUpdateIntentService? The most probable user Activity is still (50)

--Then, the system kills the IntentService?
19:41:19.704      969-979/? I/ActivityManager? Killing 13679:tripcomputer/u0a152 (adj 0): remove task
19:41:19.706    1258-4401/? W/ContextImpl? Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1244 android.content.ContextWrapper.sendBroadcast:365 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.ProcessKillTrigger.sendTrigger:147 com.motorola.motocare.internal.frameworkevents.ProcessKillTrigger.handleFrameworkEvent:164

--Boom!, everything else has gone but there's nothing in the Log
--System schedules the Crashed Services for restart
19:41:19.727     969-1273/? W/ActivityManager? Scheduling restart of crashed service tripcomputer/.services.JourneyService in 1000ms
19:41:19.736     969-1273/? W/ActivityManager? Scheduling restart of crashed service tripcomputer/.services.ActivityService in 1000ms

1 个答案:

答案 0 :(得分:0)

我最终找到了一种有效的替代方法......

我上面遇到的问题可能与处理IntentService线程的方式有关,尽管不幸的是,当意图服务的handleIntent方法完成时,我没有找到有关正在运行的应用程序完全丢失的更具体的解释。 / p>

解决方法是切换到使用常规Service的组合来处理ActivityRecognition PendingIntents(在onStartCommand(Intent ...)中)以及自定义的Runnable线程来处理从UI线程脱机的实际工作。如果您这样做,应用程序将不再意外退出,您将获得所需的效果。

有趣的是,Google Play服务的“活动识别”功能的文档和示例代码仅提及使用Android活动或片段进行用户活动识别而从未使用过服务。是否可以使用IntentServices进行活动识别只能从UI线程上运行的组件中完成???

相关问题