Because most of the started services don't need to handle multiple requests simultaneously (which can actually be a dangerous multi-threading scenario), it's best that you implement your service using the IntentService
class.
翻译:大多数情况下我们不需要同时处理多个请求,所以用IntentService是最好的
The IntentService
class does the following:
- It creates a default worker thread that executes all of the intents that are delivered to
onStartCommand()
, separate from your application's main thread. - Creates a work queue that passes one intent at a time to your
onHandleIntent()
implementation, so you never have to worry about multi-threading. - Stops the service after all of the start requests are handled, so you never have to call
stopSelf()
. - Provides a default implementation of
onBind()
that returns null. - Provides a default implementation of
onStartCommand()
that sends the intent to the work queue and then to youronHandleIntent()
implementation.
IntentService做到了以下几项
(1)创建了一个工作线程
(2)创建了一个工作队列
(3)用完自己结束
(4)提供默认onBind()返回null
(5)提供onStartCommand()的默认实现,把intent发到队列中排队,然后到onHandleIntent中
To complete the work that is provided by the client, implement onHandleIntent()
. However, you also need to provide a small constructor for the service.
为了完成工作,实现onHandlerIntent,此外,还有提供一个构造方法
Here's an example implementation of IntentService
:
示例如下
public class HelloIntentService extends IntentService {
/**
* A constructor is required, and must call the super <code><a href="/reference/android/app/IntentService.html#IntentService(java.lang.String)">IntentService(String)</a></code>
* constructor with a name for the worker thread.
*/
public HelloIntentService() {
super("HelloIntentService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
}
}
That's all you need: a constructor and an implementation of onHandleIntent()
.
两个事:构造方法以及实现onHandleIntent
If you decide to also override other callback methods, such as onCreate()
, onStartCommand()
, or onDestroy()
, be sure to call the super implementation so that the IntentService
can properly handle the life of the worker thread.
如果要override其他方法,记得call super
For example, onStartCommand()
must return the default implementation, which is how the intent is delivered to onHandleIntent()
:
例如,onStartCommand 就要 return默认实现,默认实现里面会把intent deliver到onHandleIntent
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
Besides onHandleIntent()
, the only method from which you don't need to call the super class is onBind()
. You need to implement this only if your service allows binding.
onBind不用call super,但是如果要支持binding则要自己实现onBind
In the next section, you'll see how the same kind of service is implemented when extending the base Service
class, which uses more code, but might be appropriate if you need to handle simultaneous start requests.
下面讲继承Service怎么实现,这会代码多点,但是如果要同时处理多个请求的话,可以用这个。