最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Android Service服務的高級技巧

 更新時間:2018年07月04日 14:24:37   作者:deniro  
這篇文章主要介紹了淺談Android 服務的高級技巧,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1 前臺服務

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

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

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("梅西生涯最大尷尬 戰(zhàn)法國能否破荒?")
        .setContentText("世界杯1/8決賽,法國對陣阿根廷,法國隊主帥德尚將迎來80戰(zhàn)里程碑,成為隊史執(zhí)教場次最多的主教練,高盧雄雞能否保持過去40年世界杯遇南美球隊不敗的金身,格里茲曼能否找回最佳狀態(tài),梅西能否打破此前世界杯淘汰賽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() 讓當前服務變?yōu)橐粋€前臺服務。

startForeground 接收兩個參數:

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

效果:

2 IntentService

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

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

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

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

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

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

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

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

我們新建一個類,讓它繼承自 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());
    //在此執(zhí)行耗時邏輯
  }

  @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 服務類開啟了一個新的線程來執(zhí)行耗時邏輯,并且在執(zhí)行完畢后自動停止。是不是很方便呀O(∩_∩)O哈哈~

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

相關文章

最新評論

漠河县| 长岛县| 天峨县| 砚山县| 南溪县| 襄城县| 宁武县| 湘阴县| 仁寿县| 海宁市| 乐东| 邛崃市| 洞头县| 常山县| 泉州市| 长海县| 阿坝县| 宁武县| 新乐市| 如皋市| 札达县| 尖扎县| 深圳市| 山阳县| 延庆县| 静安区| 丹江口市| 景东| 孟州市| 商城县| 绵竹市| 即墨市| 庄河市| 丹寨县| 瑞丽市| 修文县| 河池市| 竹北市| 黑龙江省| 卫辉市| 略阳县|