Android 中 Service 與 IntentService 的區別


簡介

Service 是 Android 中的一個元件,它在後臺執行以執行特定任務或長時間執行的操作,而無需與使用者介面互動。Service 的主要目標是確保服務在後臺保持活動狀態,並且使用者可以同時使用多個應用程式。

以下是在 Android 中實現服務的步驟

  • 建立 Service 類:第一步是建立一個擴充套件 android.app.Service 類的 Service 類。此類將定義服務的行為。

  • 啟動服務:您需要使用一個 Intent 呼叫 startService() 方法來標識您要啟動的服務。您可以藉助 BroadcastReceiver 或 Activity 來完成此操作。

  • 停止服務:您需要使用一個 Intent 呼叫 stopService() 方法來標識您要停止的服務。您也可以在 Service 類中呼叫 stopSelf() 方法來停止服務。

  • 繫結服務:如果您希望從 Activity 或 BroadcastReceiver 與服務進行互動,則可以使用 bindService() 方法繫結服務。這允許我們透過介面與服務進行通訊。

  • 解除繫結服務:在實現服務後,如果您想解除繫結服務,則應使用 unbindService() 方法解除繫結。

  • 處理服務生命週期:正確處理服務生命週期對於避免浪費資源非常必要。您可以透過在 Service 類中實現 **onCreate()、onStartCommand()、onBind()** 和 **onDestroy()** 方法來做到這一點。

以下是如何在 Android 中實現服務的程式碼

package com.example.java_test_application;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
   @Override
   public void onCreate() {
       super.onCreate();
       // code to initialize the Service
   }
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
       // code to perform background tasks
       return START_STICKY;
   }
   @Override
   public IBinder onBind(Intent intent) {
       // code to bind the Service
       return null;
   }
   @Override
   public void onDestroy() {
       super.onDestroy();
       // code to stop the Service and release resources
   }
}

要啟動服務,您可以使用一個 Intent 呼叫 **startService()** 方法來標識您要啟動的服務。

Intent intent = new Intent(this, MyService.class);
startService(intent);

要停止服務,您可以使用一個 Intent 呼叫 **stopService()** 方法來標識您要停止的服務。

Intent intent = new Intent(this, MyService.class);
stopService(intent);

什麼是 Android 中的 Service?

Android 中的 Service 用於在後臺執行特定任務,無論應用程式是否可見。它主要用於執行長時間執行的操作。服務使用 **startService()** 方法啟動,並使用 **stopService()** 方法停止。

  • **前臺服務** - 對使用者可見的服務,或者通知使用者正在進行的任務的服務,例如下載檔案,使用者可以看到檔案正在下載,他可以暫停或恢復檔案下載,這被稱為前臺服務。

  • **後臺服務** - 這些服務不需要任何使用者互動,它們本身在後臺執行。此服務不會通知使用者正在進行的任務。資料的儲存屬於後臺服務,使用者不知道資料是如何儲存的。

  • **繫結服務** - 此服務允許應用程式元件繫結到自身。繫結服務只要任何應用程式元件繫結到它,就會執行其任務。您可以使用 bindService() 方法繫結應用程式的元件。

什麼是 Android 中的 IntentService?

Intent Service 用於在後臺執行非同步任務。並且一旦處理完提供給它的所有任務,它就會自行停止。它主要用於執行需要在單獨執行緒上執行的簡短操作。Intent Service 不會在前臺執行。

在 Android 中實現 Intent Service

步驟 1:在 AndroidManifest.xml 檔案中指定 Intent Service

轉到 AndroidManifest.xml 檔案,並在 <application> 元素中宣告服務。

<service
   android:name=".SampleIntentService"
   android:exported="false" />

步驟 2:建立一個名為 SampleIntentService 的類,該類擴充套件 Intentservice

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.annotation.Nullable;
public class SampleIntentService extends IntentService {
   // Constructor
   public SampleIntentService() {
      // Call superclass constructor with
      // the name of the worker thread
      super("SampleIntentService");
   }
   @Override
   protected void onHandleIntent(@Nullable Intent intent) {
      // Perform a task in the background
      Log.d("SampleIntentService", "Task in progress");
      // Simulate a long running task by
      // sleeping the thread for 5 seconds
      try {
         Thread.sleep(2000);
      } catch (InterruptedException e) {
         // InterruptedException occurs
         e.printStackTrace();
      }
      Log.d("SampleIntentService", "Task has been executed");
   }
}

覆蓋 SampleIntentService 類的 **onHandleIntent()** 方法,以指定您想要在後臺執行的任務。

Android 中 Service 與 IntentService 的區別

Service

IntentService

Service 是一個可用於建立服務的基類。它是一個通用類,可以在後臺執行長時間執行的操作。

IntentService 是 Service 的子類,它使用工作執行緒按需處理非同步請求(表示為 Intent)。

Service 可用於長時間和短時間任務。

IntentService 用於不需要再次執行的長時間執行的任務。

Service 可以同時處理多個請求。

IntentService 一次處理一個請求,並在請求完成後自行停止。

Service 在應用程式的主執行緒中執行。

IntentService 在單獨的工作執行緒中執行。

Service 可以根據需要停止和重新啟動。

IntentService 停止後無法重新啟動。

Service 可以有多個啟動命令。

IntentService 只有一個啟動命令。

Service 可以將結果返回給呼叫方。

IntentService 不會將結果返回給呼叫方。

結論

總而言之,Service 用於執行在後臺執行的長時間執行的操作。而 Intent Service 用於執行在單獨執行緒上執行的短暫操作。

更新於: 2023年5月9日

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.