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

淺談Android Service服務(wù)的高級(jí)技巧

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

1 前臺(tái)服務(wù)

因?yàn)榉?wù)的優(yōu)先級(jí)較低,所以當(dāng)系統(tǒng)內(nèi)存不足時(shí),可能會(huì)回收正在后臺(tái)運(yùn)行的服務(wù)。如果若要避免服務(wù)被回收,可以使用前臺(tái)服務(wù)。

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

public class FirstService extends Service {

  private static final String TAG = "FirstService";

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

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

在此構(gòu)建出通知對(duì)象(Notification)之后,調(diào)用 startForeground() 讓當(dāng)前服務(wù)變?yōu)橐粋€(gè)前臺(tái)服務(wù)。

startForeground 接收兩個(gè)參數(shù):

參數(shù) 說明
id 通知 ID
Notification Notification 對(duì)象

效果:

2 IntentService

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

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

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

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

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

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

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

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

我們新建一個(gè)類,讓它繼承自 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 服務(wù)): " + Thread.currentThread().getId());
    //在此執(zhí)行耗時(shí)邏輯
  }

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

注意:這個(gè)類必須提供一個(gè)無參構(gòu)造函數(shù),并且必須在這個(gè)構(gòu)造函數(shù)內(nèi)部調(diào)用父類的有參構(gòu)造函數(shù)。

接著,在活動(dòng)類中啟動(dòng) Intent 服務(wù):

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

輸出結(jié)果:

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

玛多县| 富阳市| 榆社县| 尖扎县| 永城市| 九江县| 张家界市| 连南| 东明县| 大厂| 清水县| 江川县| 沙洋县| 永仁县| 化德县| 广元市| 渭南市| 土默特右旗| 永新县| 太康县| 应城市| 大庆市| 阜新市| 松原市| 郑州市| 永泰县| 长乐市| 崇明县| 临猗县| 麟游县| 沙雅县| 正阳县| 新郑市| 集贤县| 米泉市| 翁源县| 饶河县| 嘉祥县| 黄大仙区| 清涧县| 肥西县|