Service 和 Activity 属于同一级别的应用组件,默认情况下它运行在宿主进程的主线程中,因此,不要因为 Service 没有运行在前台,就误以为它可以毫无条件地执行耗时工作(CPU intensive work),否则将导致 ANR。
而 IntentService 却是个例外,它开辟了一个后台工作线程来执行耗时工作,你可以将离线的耗时工作委托给 IntentService 去完成而不必阻碍主线程循环(main thread loop)。
你可以通过 LocalBroadCastManager 将耗时工作的结果或运行状态广播给进程内部启动该服务的 Activity 或者用 Notification 机制发送通知。
思考:我们使用的 android APP 中不乏很多 Notification 消息通过状态栏通知我们,即使你退出了这个 APP,这可以用什么机制来实现?
关于 IntentService 的更多内容请参考 SDK 文档:
https://developer.android.com/training/run-background-service/index.html
https://developer.android.com/reference/android/app/IntentService.html