亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用IntentService進(jìn)行apk更新示例代碼

 更新時(shí)間:2018年01月30日 08:36:46   作者:momentslz  
這篇文章主要介紹了Android使用IntentService進(jìn)行apk更新示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

通常在使用service更新應(yīng)用時(shí)最常出現(xiàn)的問(wèn)題就是Notification進(jìn)度的更新問(wèn)題、service在什么時(shí)間關(guān)閉以及需要我們自己在Service中創(chuàng)建新的線程處理耗時(shí)操作,當(dāng)然這種也是可以實(shí)現(xiàn)的但是會(huì)顯得略微繁瑣

經(jīng)過(guò)對(duì)比發(fā)現(xiàn)可以使用IntentService已經(jīng)實(shí)現(xiàn)了對(duì)耗時(shí)操作的包裝出來(lái),我們只需要實(shí)現(xiàn)IntentService中的onHandleIntent方法就可以在其中進(jìn)行耗時(shí)操作的處理,在處理下載問(wèn)題時(shí)發(fā)現(xiàn)在使用intentservice時(shí)暫時(shí)沒(méi)有發(fā)現(xiàn)可以?xún)?yōu)雅的進(jìn)行進(jìn)度回調(diào)的實(shí)現(xiàn)方法,所以我這邊使用了本地廣播的形式來(lái)進(jìn)行進(jìn)度刷新。

添加了當(dāng)前狀態(tài)判斷,當(dāng)應(yīng)用處于前臺(tái)狀態(tài)時(shí)直接進(jìn)行安裝,當(dāng)應(yīng)用處于后臺(tái)時(shí)彈出notification彈窗點(diǎn)擊后安裝,示例如下圖:


先創(chuàng)建廣播

public static class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      switch (intent.getAction()) {
        case ACTION_TYPE_PREPARE:
          if (downloadCallback != null) {
            downloadCallback.onPrepare();
          }
          break;
        case ACTION_TYPE_PROGRESS:
          int progress = intent.getIntExtra("progress", 0);
//          Log.d("progress", "|- " + progress + " -|");
          if (downloadCallback != null) {
            downloadCallback.onProgress(progress);
          }
          break;
        case ACTION_TYPE_COMPLETE:
          String file_path = intent.getStringExtra("file_path");
          if (!TextUtils.isEmpty(file_path)) {
            File file = new File(file_path);
            if (file.exists()) {
              if (downloadCallback != null) {
                downloadCallback.onComplete(file);
              }
            }
          }
          break;
        case ACTION_TYPE_FAIL:
          String error = intent.getStringExtra("error");
          if (downloadCallback != null) {
            downloadCallback.onFail(error + "");
          }
          break;
      }
    }

然后在IntentService中初始化本地廣播并發(fā)送信息

@Override
  public void onCreate() {
    super.onCreate();
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
  }
  
  // 在下載進(jìn)度刷新的地方進(jìn)行回調(diào)
  private void progress(int progress) {
    Intent intent = new Intent(FileDownloaderManager.ACTION_TYPE_PROGRESS);
    intent.putExtra("progress", progress);
    mLocalBroadcastManager.sendBroadcast(intent);
  }
  
  private void downApk(String url) {
  .....
  .....
   progress(progress);
  .....
  .....
  }

在activity中使用

mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_TYPE_PREPARE);
intentFilter.addAction(ACTION_TYPE_PROGRESS);
intentFilter.addAction(ACTION_TYPE_COMPLETE);
intentFilter.addAction(ACTION_TYPE_FAIL);
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
// ondestory時(shí)調(diào)用
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);

以上源碼已進(jìn)行封裝,方便使用具體操作步驟如下:

|- 初始化及注冊(cè)回調(diào)

//初始化文件下載管理類(lèi)
FileDownloaderManager.init(context)
// 注冊(cè)下載進(jìn)度監(jiān)聽(tīng),并開(kāi)啟廣播接收
FileDownloaderManager.registerDownload(object : FileDownloaderManager.DownloadCallback {
      override fun onComplete(file: File) = mainView.downloadSucc(file)

      override fun onFail(msg: String?) = Unit

      override fun onProgress(progress: Int) = mainView.onProgress(progress)

      override fun onPrepare() = Unit

    })
//開(kāi)始下載
FileDownloaderManager.download(url)

|- 在下載完成后進(jìn)行資源重置

FileDownloaderManager.unbinder()

源碼地址:源碼地址 

文檔地址:文檔地址

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

相關(guān)文章

最新評(píng)論