溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

淺談Android Service服務的高級技巧

發布時間:2020-10-09 18:59:58 來源:腳本之家 閱讀:201 作者:deniro 欄目:移動開發

1 前臺服務

因為服務的優先級較低,所以當系統內存不足時,可能會回收正在后臺運行的服務。如果若要避免服務被回收,可以使用前臺服務。

前臺服務會一直有一個圖標在系統的狀態欄中顯示,下拉狀態欄可以看到更加詳細的信息,類似于消息通知效果。

public class FirstService extends Service {

  private static final String TAG = "FirstService";

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate");

    //設置為前臺服務
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("梅西生涯最大尷尬 戰法國能否破荒?")
        .setContentText("世界杯1/8決賽,法國對陣阿根廷,法國隊主帥德尚將迎來80戰里程碑,成為隊史執教場次最多的主教練,高盧雄雞能否保持過去40年世界杯遇南美球隊不敗的金身,格里茲曼能否找回最佳狀態,梅西能否打破此前世界杯淘汰賽666分鐘的進球荒,都是此役的關鍵看點。")
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
        .setContentIntent(pendingIntent)
        .build();
    startForeground(1,notification);
  }
}

在此構建出通知對象(Notification)之后,調用 startForeground() 讓當前服務變為一個前臺服務。

startForeground 接收兩個參數:

參數 說明
id 通知 ID
Notification Notification 對象

效果:

淺談Android Service服務的高級技巧

2 IntentService

如果在服務中處理耗時操作,那么容易出現 ANR(Application Not Responding)問題。

為了避免我們可以在主服務的具體方法中開啟子線程,然后在子線程中來執行耗時操作,形如:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  Log.d(TAG, "onStartCommand");

  //在子線程中來執行耗時操作
  new Thread(new Runnable() {
    @Override
    public void run() {
      //耗時操作
    }
  }).start();

  return super.onStartCommand(intent, flags, startId);
}

這樣的服務一旦啟動后,就會一直處于運行狀態,直到調用 stopService() 或者 stopSelf() 才會停止服務。我們可以在耗時操作執行完畢后,調用 stopSelf() ,讓服務自行停止:

new Thread(new Runnable() {
  @Override
  public void run() {
    //耗時操作
     stopSelf();
  }
}).start();

Android 提供了 IntentService 類,可以直接創建一個異步、執行完畢會自行結束的服務。

我們新建一個類,讓它繼承自 IntentService :

public class SecondService extends IntentService {
  private static final String TAG = "SecondService";

  public SecondService() {
    super("SecondService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "子線程 id(Intent 服務): " + Thread.currentThread().getId());
    //在此執行耗時邏輯
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
  }
}

注意:這個類必須提供一個無參構造函數,并且必須在這個構造函數內部調用父類的有參構造函數。

接著,在活動類中啟動 Intent 服務:

Log.d(TAG, "主線程 id: " + Thread.currentThread().getId());
Intent intentService = new Intent(context, SecondService.class);
startService(intentService);

輸出結果:

D/MainActivity: 主線程 id: 1
D/SecondService: 子線程 id(Intent 服務): 145
D/SecondService: onDestroy

從結果中可以看出,IntentService 服務類開啟了一個新的線程來執行耗時邏輯,并且在執行完畢后自動停止。是不是很方便呀O(∩_∩)O哈哈~

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女