Retrofit2實現App自動更新

原理

  • Retrofit2和okhttp實現了apk的下載
  • 自定義類實現Retrofit2的Callback類在裏面通過IO流寫入文件並且使用RxBus訂閱下載進度
  • 自定義類實現okhttp3的ResponseBody類並且在裏面使用RxBus發佈下載進度信息
  • 在Service中使用Retrofit在後臺下載文件
  • 發送Notifaction到通知欄前臺界面展示進度情況

實現步驟

1.創建UpdateManger管理類

這個類主要寫了兩個管理更新和彈框的方法。

/**
 - 檢測軟件更新
   */
  public void checkUpdate(final boolean isToast) {
      /**
       * 在這裏請求後臺接口,獲取更新的內容和最新的版本號
       */
      // 版本的更新信息
      String version_info = "更新內容\n" + "    1. 車位分享異常處理\n" + "    2. 發佈車位折扣格式統一\n" + "    ";
      int mVersion_code = DeviceUtils.getVersionCode(mContext);// 當前的版本號
      int nVersion_code = 2;
      if (mVersion_code < nVersion_code) {
          // 顯示提示對話
          showNoticeDialog(version_info);
      } else {
          if (isToast) {
              Toast.makeText(mContext, "已經是最新版本", Toast.LENGTH_SHORT).show();
          }
      }
  }

    /**
     * 顯示更新對話框
     *
     * @param version_info
     */
    private void showNoticeDialog(String version_info) {
        // 構造對話框
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("更新提示");
        builder.setMessage(version_info);
        // 更新
        builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                // 啓動後臺服務下載apk
                mContext.startService(new Intent(mContext, DownLoadService.class));
            }
        });
        // 稍後更新
        builder.setNegativeButton("以後更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        Dialog noticeDialog = builder.create();
        noticeDialog.show();
    }

2.初始化RxBus進行簡單封裝

import rx.Observable;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;

public class RxBus {

    private static volatile RxBus mInstance;
    private final Subject<Object, Object> bus;

    private RxBus() {
        bus = new SerializedSubject<>(PublishSubject.create());
    }

    /**
     * 單例RxBus
     *
     * @return
     */
    public static RxBus getDefault() {
        RxBus rxBus = mInstance;
        if (mInstance == null) {
            synchronized (RxBus.class) {
                rxBus = mInstance;
                if (mInstance == null) {
                    rxBus = new RxBus();
                    mInstance = rxBus;
                }
            }
        }
        return rxBus;
    }

    /**
     * 發送一個新事件
     *
     * @param o
     */
    public void post(Object o) {
        bus.onNext(o);
    }

    /**
     * 返回特定類型的被觀察者
     *
     * @param eventType
     * @param <T>
     * @return
     */
    public <T> Observable<T> toObservable(Class<T> eventType) {
        return bus.ofType(eventType);
    }

}

3.自己定製ResponseBody類FileResponseBody

public class FileResponseBody extends ResponseBody {

    Response originalResponse;

    public FileResponseBody(Response originalResponse) {
        this.originalResponse = originalResponse;
    }

    @Override
    public MediaType contentType() {
        return originalResponse.body().contentType();
    }

    @Override
    public long contentLength() {// 返回文件的總長度,也就是進度條的max
        return originalResponse.body().contentLength();
    }

    @Override
    public BufferedSource source() {
        return Okio.buffer(new ForwardingSource(originalResponse.body().source()) {
            long bytesReaded = 0;

            @Override
            public long read(Buffer sink, long byteCount) throws IOException {
                long bytesRead = super.read(sink, byteCount);
                bytesReaded += bytesRead == -1 ? 0 : bytesRead;
                // 通過RxBus發佈進度信息
                RxBus.getDefault().post(new FileLoadingBean(contentLength(), bytesReaded));
                return bytesRead;
            }
        });
    }
}

4.自己定製Callback類FileCallback

一個抽象類實現了Callback的onResponse方法並且在裏面利用IO流把文件保存到了本地
定義了兩個抽象方法讓子類實現,onSuccess()當讀寫完成之後將文件回調給實現類以便安裝apk,onLoading()在文件讀寫的過程中通過訂閱下載的進度把進度信息progress和total回調給實現類以便在通知中實時顯示進度信息

public abstract class FileCallback implements Callback<ResponseBody>{
    /**
     * 訂閱下載進度
     */
    private CompositeSubscription rxSubscriptions = new CompositeSubscription();
    /**
     * 目標文件存儲的文件夾路徑
     */
    private String destFileDir;
    /**
     * 目標文件存儲的文件名
     */
    private String destFileName;

    public FileCallback(String destFileDir, String destFileName) {
        this.destFileDir = destFileDir;
        this.destFileName = destFileName;
        subscribeLoadProgress();// 訂閱下載進度
    }
    /**
     * 成功後回調
     */
    public abstract void onSuccess(File file);

    /**
     * 下載過程回調
     */
    public abstract void onLoading(long progress, long total);

    /**
     * 請求成功後保存文件
     */
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        try {
            saveFile(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 通過IO流寫入文件
     */
    public File saveFile(Response<ResponseBody> response) throws Exception {
        InputStream in = null;
        FileOutputStream out = null;
        byte[] buf = new byte[2048];
        int len;
        try {
            File dir = new File(destFileDir);
            if (!dir.exists()) {// 如果文件不存在新建一個
                dir.mkdirs();
            }
            in = response.body().byteStream();
            File file = new File(dir,destFileName);
            out = new FileOutputStream(file);
            while ((len = in.read(buf)) != -1){
                out.write(buf,0,len);
            }
            // 回調成功的接口
            onSuccess(file);
            unSubscribe();// 取消訂閱
            return file;
        }finally {
            in.close();
            out.close();
        }
    }
    /**
     * 訂閱文件下載進度
     */
    private void subscribeLoadProgress() {
        rxSubscriptions.add(RxBus.getDefault()
                .toObservable(FileLoadingBean.class)// FileLoadingBean保存了progress和total的實體類
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<FileLoadingBean>() {
                    @Override
                    public void call(FileLoadingBean fileLoadEvent) {
                        onLoading(fileLoadEvent.getProgress(), fileLoadEvent.getTotal());
                    }
                }));
    }
    /**
     * 取消訂閱,防止內存泄漏
     */
    private void unSubscribe() {
        if (!rxSubscriptions.isUnsubscribed()) {
            rxSubscriptions.unsubscribe();
        }
    }
}

保存了progress和total的實體類

public class FileLoadingBean {
    /**
     * 文件大小
     */
    long total;
    /**
     * 已下載大小
     */
    long progress;

    public long getProgress() {
        return progress;
    }

    public long getTotal() {
        return total;
    }

    public FileLoadingBean(long total, long progress) {
        this.total = total;
        this.progress = progress;
    }
}

5.在後臺Service中利用Retrofit2和okhttp下載並安裝apk,同時發送通知在前臺展示下載進度

public class DownLoadService extends Service {

    /**
     * 目標文件存儲的文件夾路徑
     */
    private String  destFileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File
            .separator + "M_DEFAULT_DIR";
    /**
     * 目標文件存儲的文件名
     */
    private String destFileName = "shan_yao.apk";

    private Context mContext;
    private int preProgress = 0;
    private int NOTIFY_ID = 1000;
    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;
    private Retrofit.Builder retrofit;
    /**
     * 爲什麼在這個方法調用下載的邏輯?而不是onCreate?我在下面有解釋
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mContext = this;
        loadFile();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 下載文件
     */
    private void loadFile() {
        initNotification();
        if (retrofit == null) {
            retrofit = new Retrofit.Builder();
        }
        // 使用Retrofit進行文件的下載
        retrofit.baseUrl("http://112.124.9.133:8080/parking-app-admin-1.0/android/manager/adminVersion/")
                .client(initOkHttpClient())
                .build()
                .create(IFileLoad.class)
                .loadFile()
                .enqueue(new FileCallback(destFileDir, destFileName) {

                    @Override
                    public void onSuccess(File file) {
                        Log.e("zs", "請求成功");
                        // 安裝軟件
                        cancelNotification();
                        installApk(file);
                    }

                    @Override
                    public void onLoading(long progress, long total) {
                        Log.e("zs", progress + "----" + total);
                        updateNotification(progress * 100 / total);// 更新前臺通知
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Log.e("zs", "請求失敗");
                        cancelNotification();// 取消通知
                    }
                });
    }

    public interface IFileLoad {
        @GET("download")
        Call<ResponseBody> loadFile();
    }

    /**
     * 安裝軟件
     *
     * @param file
     */
    private void installApk(File file) {
        Uri uri = Uri.fromFile(file);
        Intent install = new Intent(Intent.ACTION_VIEW);
        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        install.setDataAndType(uri, "application/vnd.android.package-archive");
        // 執行意圖進行安裝
        mContext.startActivity(install);
    }

    /**
     * 初始化OkHttpClient
     *
     * @return
     */
    private OkHttpClient initOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(100000, TimeUnit.SECONDS);
        builder.networkInterceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse
                        .newBuilder()
                        .body(new FileResponseBody(originalResponse))//將自定義的ResposeBody設置給它
                        .build();
            }
        });
        return builder.build();
    }

    /**
     * 初始化Notification通知
     */
    public void initNotification() {
        builder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.mipmap.ic_launcher)// 設置通知的圖標
                .setContentText("0%")// 進度Text
                .setContentTitle("QQ更新")// 標題
                .setProgress(100, 0, false);// 設置進度條
        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);// 獲取系統通知管理器
        notificationManager.notify(NOTIFY_ID, builder.build());// 發送通知 
    }

    /**
     * 更新通知
     */
    public void updateNotification(long progress) {
        int currProgress = (int) progress;
        if (preProgress < currProgress) {
            builder.setContentText(progress + "%");
            builder.setProgress(100, (int) progress, false);
            notificationManager.notify(NOTIFY_ID, builder.build());
        }
        preProgress = (int) progress;
    }

    /**
     * 取消通知
     */
    public void cancelNotification() {
        notificationManager.cancel(NOTIFY_ID);
    }
}

需要注意的幾個地方

1.爲什麼在onStartCommand裏面下載apk而不是onCreate裏面

這個跟service的生命週期有關係,onStartCommand()在每次調用startService時候都會調用一次,而onCreate()方法只有在服務第一次啓動的時候纔會掉用。當一個服務已經開啓了那麼再次調用startService不會在調用onCreate()方法,而onStartCommand()會被再次調用。

2.發送通知設置進度用的是setProgress,這個方法只有在4.0以上才能用

如果不用這個方法想要兼容低版本,就只能使用自定義的Notification,然後自己創建一個含有ProgressBar的layout佈局,但是自定義的Notification在不同的系統中的適配處理太麻煩了,因爲不同的系統的通知欄的背景顏色不一致,我們需要對不同的背景做不同的適配才能保證上面的文字能夠正常顯示,比方你寫死了一個白色的文字,但是系統通知的背景也是白色,這樣一來文字就不能正常顯示了,當然如果使用系統的通知樣式無法滿足你的需求,只能使用自定義樣式,可以參考這篇文章Android自定義通知樣式適配。如果僅僅是爲了兼容低版本我個人感覺完全沒有必要,大家可以看看友盟指數,所以沒有必要去做兼容。這個看不同的需求而定。

3.在更新通知時我做了判斷

在更新通知時我做了判斷看下代碼,這裏我設置只有當進度等於1時並且發生改變時做更新,因爲progress的值可能爲1.1,1.2,1.3,但是顯示的都會是1%,如果我不做判斷限制那麼每次onLoading的回調都會更新通知內容,也就是當1.1時會更新,1.2時也會執行更新的方法,但是前臺展示的都是1%,所以做了好多無用功,並且當你不做限制時整個app會非常的卡,有時會卡死。這個你可以不做限制自己運行下Demo試試,絕對卡死。

   /**
     * 更新通知
     */
    public void updateNotification(long progress) {
        int currProgress = (int) progress;
        if (preProgress < currProgress) {
            builder.setContentText(progress + "%");
            builder.setProgress(100, (int) progress, false);
            notificationManager.notify(NOTIFY_ID, builder.build());
        }
        preProgress = (int) progress;
    }

源碼 DownLoadManager

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章