我在Services/IntentServices上遇到了奇怪的Android问题...
我有一个Service,它启动一个管理类,该类从Google Play服务请求ActivityRecognition更新(即获取连接,然后调用ActivityRecognitionClient上的requestActivityUpdates(),传递PendingIntent)。
PendingIntent引用一个IntentService,并实现了onHandleIntent(Intent)——它只是将最可能的当前物理活动打印到日志中。
到目前为止都很好。一切正常——Service被绑定,Manager连接成功,IntentService被触发,用户的物理活动被写入日志。
这里出现了问题...
我希望这个Service只需在后台侦听活动更新,即使我的用户关闭了“最近使用的应用程序”屏幕中的UI活动也是如此。我已经在我的应用程序中使用了此技术来进行位置更新——我的位置服务即使用户或系统销毁了应用程序的UI部分仍然会跟踪位置。
问题是,自从添加了这个新的Activity recognition功能后,当用户杀死UI时,下一个PendingIntent接收到的IntentService就会杀死整个应用程序,包括所有其他服务。一切都停止/死亡/消失了。但没有警告、异常、日志条目或任何东西!
唯一继续工作的是该可恶的IntentService!每次收到新的Intent时,它都像没发生过任何事情一样继续运行。
如果我注释掉注册活动更新的代码(因此不使用PendingIntent并且不调用IntentService),一切都正常。
我不知道可能导致这种崩溃,也没有日志中的线索。
我在Google/SO上搜索了很久,但没有找到任何描述类似行为的人。
那么除了IntentService之外,是否还有其他类型的Android组件可以通过Intent触发?IntentService是真正的问题吗?Activity recognition不能像这样在后台完成吗?我还能尝试什么其他方法?
更新: 当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
onStartCommand()
中编写的代码添加上?我想了解你是如何将它们结合起来的。 - Darpan