溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

本地Service

發(fā)布時(shí)間:2020-06-28 07:14:39 來(lái)源:網(wǎng)絡(luò) 閱讀:205 作者:just2012xia 欄目:移動(dòng)開(kāi)發(fā)

通過(guò)在content(如activity)中調(diào)用startService(Intent )啟動(dòng)service,service只有在調(diào)用stopSelf(),或者另外component調(diào)用stopService()時(shí)才會(huì)停止。

service啟動(dòng)時(shí)會(huì)自動(dòng)調(diào)用

onStartCommand(Intent intent, int flags, int startId)

Service類(lèi):

This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running

IntentService類(lèi):

This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.

IntentService類(lèi)做如下工作:

  • Creates a default worker thread that executes all intents 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 start requests have been handled, so you never have to call stopSelf().

  • Provides default implementation of onBind() that returns null.

  • Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation

所以,只需實(shí)現(xiàn)onHandleIntent()函數(shù),和構(gòu)造函數(shù),不比考慮多線(xiàn)程的問(wèn)題


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) {
              }
          }
      }
  }
}

當(dāng)重載其他回調(diào)函數(shù)時(shí),記住super

@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);
}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI