IntentService(Extending the IntentService class)

http://developer.android.com/guide/topics/fundamentals/services.html

Extendingthe IntentService class

Because most started services don't need tohandle multiple requests simultaneously (which can actually be a dangerousmulti-threading scenario), it's probably best if you implement your serviceusing the IntentService class.

由于大部分启动的service,并不需要同时处理多个请求(这样会产生一个危险的多线程场景),所以最好是通过使用IntentService 类来实现你的service。


The IntentService does the following:

1.Creates a default worker thread thatexecutes all intents delivered to onStartCommand() separatefrom your application's main thread.

脱离主线程,创建一个默认的“workerthread”线程来执行所有发送到onStartCommand()的intent。

2.Creates a work queue that passes one intentat a time to your onHandleIntent() implementation,so you never have to worry about multi-threading.

创建一个“workqueue”队列一次传递一个intent给你的 onHandleIntent()函数,所以你不用担心多线程问题。

3.Stops the service after all start requestshave been handled, so you never have to call stopSelf().

在所有的开始的请求被处理了之后自动停止service,所以你不必调用stopSelf()

4.Provides default implementation of onBind() thatreturns null.

提供onBind()的默认实现并返回null

5.Provides a default implementation of onStartCommand() thatsends the intent to the work queue and then to your onHandleIntent() implementation.

提供onStartCommand() 的默认实现,它会将intent送往“work queue”队列,然后送往你的 onHandleIntent()函数。


All this adds up to the fact that all youneed to do is implement onHandleIntent() todo the work provided by the client. (Though, you also need to provide a smallconstructor for the service.)

你需要做的事情就是实现onHandleIntent()函数(虽然,你还需要给service提供一个小的构造函数)。

Here's an example implementation of IntentService:

public class HelloIntentService extends IntentService {

  /** 
   * A constructor is required, and must call the super IntentService(String)
   * 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.
      long endTime = System.currentTimeMillis() + 5*1000;
      while (System.currentTimeMillis() < endTime) {
          synchronized (this) {
              try {
                  wait(endTime - System.currentTimeMillis());
              } catch (Exception e) {
              }
          }
      }
  }
}

That's all you need: a constructor and animplementation of onHandleIntent().

你所需要做的:一个构造函数和onHandleIntent()的实现


If you decide to also override othercallback methods, such as onCreate()onStartCommand(),or onDestroy(),be sure to call the super implementation, so that the IntentService canproperly handle the life of the worker thread.

如果你打算重写其他回调函数,例如 onCreate()onStartCommand(),或者是 onDestroy(),确保调用超类的实现,以便IntentService能够适当的处理“worker thread”线程的生命周期。


For example, onStartCommand() mustreturn the default implementation (which is how the intent gets deliveredto onHandleIntent()):

例如,onStartCommand() 方法必须返回一个默认实现(该实现的作用是将intent发送给 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() (butyou only need to implement that if your service allows binding).

除了 onHandleIntent()函数,你唯一不需要调用超类实现的函数时onBind() (只有当你的service允许绑定时,你才需要实现它)。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值