---
title: "service启动的两种方式和IntentService"
author: "jguangyou"
date: "2014-05-04T11:47:25Z"
tags: []
categories: ["移动开发"]
slug: "service-intentservice"
draft: false
lastmod: "2014-05-08T15:51:58Z"
id: 2059565
url: "https://deep-fish.iteye.com/blog/2059565"
---

启动一个service有两种方式：

- 通过Context的startService()方法：通过该方法启动service，访问者与service之间没有关联，即使访问者退出了，Service仍然正常运行。
- 通过Context的bindService()方法：使用该方法启动service，访问者与Service绑定在一起，访问者一旦退出，Service也就终止。

 

 

Caution：一个service运行在宿主进程的主线程中——service不创建自己的线程，并且不运行在一个单独的进程中。意思就是说，如果你的service打算做一些使用cpu密集的工作或者阻塞的操作（例如MP3播放或者网络操作），你必须创建一个新的线程去做一些工作。使用一个单独的线程，你可以减少出现ANR的几率，并且应用的主线程可以专注于和用户交互。

 

 

上面说了，在service线程是在主线程中运行的，所以不能执行耗时任务，如果要执行耗时任务可以在onStartCommand方法中启动一个子线程，其实service还提供了另一个方案，那就是IntentService，他是Service的子类。下面翻译一些官方的文档，对IntentService做个介绍。

 

[IntentService](http://developer.android.com/reference/android/app/IntentService.html)  是Service的子类，他使用一个工作线程去处理所有的请求，每次处理一个。如果你不需要大量的同步请求，这是一个非常好的选项。你要做的就是实现[onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent%28android.content.Intent%29)方法，他接受intent请求，并在后台线程中处理。

 

[IntentService](http://developer.android.com/reference/android/app/IntentService.html)做一下的事情：

- 创建一个默认的工作线程，来处理所发送到[onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 的intent请求，并且和你的主线程分开。
- 创建一个工作队列，并每次传送一个intent到[onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent%28android.content.Intent%29)方法，所以，你从来不用担心多线程的问题。
- 到任务执行完成，service自动停止，所有你从来不用调用 `stopSelf()`.方法
- 提供[onBind()](http://developer.android.com/reference/android/app/IntentService.html#onBind%28android.content.Intent%29)的默认实现，并返回null。
- 提供`onStartCommand()` 方法的默认实现，并把intent传递给工作队列，然后发送给[onStartCommand()](http://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29),。

关于[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 方法的返回值：

此方法的返回值一定是一个整数，系统根据这个值来决定当[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 方法执行完成后，系统由于某种原因关闭服务后将如何处理。返回值为如下几个：

- [START\_NOT\_STICKY](http://developer.android.com/reference/android/app/Service.html#START_NOT_STICKY) 如果[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 执行完毕后，系统意外关闭服务，系统不会重启服务。
- [START\_STICKY](http://developer.android.com/reference/android/app/Service.html#START_STICKY)  如果[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 执行完毕后，系统意外关闭服务，会重新创建服务并调用[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 方法，但是不会重新发送intent，而是给[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 一个null的intent。
- [START\_REDELIVER\_INTENT](http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT) 如果[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 执行完毕后，系统意外关闭服务，会重新创建服务并调用[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 方法，但是不会重新发送intent，而是给[onStartCommand()](http://developer.android.com/reference/android/app/IntentService.html#onStartCommand%28android.content.Intent,%20int,%20int%29) 一个最后发送的intent。