一個越來越強大的android版本檢測更新庫

CheckVersionLib

最新文檔請看github

V2版震撼來襲,功能強大,鏈?zhǔn)骄幊?,調(diào)用簡單,集成輕松,擴展性強大

老規(guī)矩先看V2效果,這個版本最大的特點就是使用非常簡單,相對于1.+版本

效果

v2.png
v2.gif

特點

  • [x] 任何地方都可以調(diào)用

  • [x] 簡單簡單簡單簡單(重要的話我說四遍)

  • [x] 擴展性強大

  • [x] 所有具有升級功能的app均可使用,耶穌說的

  • [x] 更強大的自定義界面支持

  • [x] 支持強制更新(一行代碼)

  • [x] 支持靜默下載 (一行代碼)

  • [x] 適配到Android O

導(dǎo)入

compile 'com.allenliu.versionchecklib:library:2.0.0'

使用

和1.+版本一樣,兩種模式

只使用下載模式

先來個最簡單的調(diào)用

        AllenVersionChecker
                .getInstance()
                .downloadOnly(
                        UIData.create().setDownloadUrl(downloadUrl)
                )
                .excuteMission(context);

UIData:UIData是一個Bundle,用于存放用于UI展示的一些數(shù)據(jù),后面自定義界面時候可以拿來用

請求服務(wù)器版本+下載

該模式最簡單的使用

   AllenVersionChecker
                .getInstance()
                .requestVersion()
                .setRequestUrl(requestUrl)
                .request(new RequestVersionListener() {
                    @Nullable
                    @Override
                    public UIData onRequestVersionSuccess(String result) {
                        //拿到服務(wù)器返回的數(shù)據(jù),解析,拿到downloadUrl和一些其他的UI數(shù)據(jù)
                        ...
                        return UIData.create().setDownloadUrl(downloadUrl);
                    }

                    @Override
                    public void onRequestVersionFailure(String message) {

                    }
                })
                .excuteMission(context);


請求版本一些其他的http參數(shù)可以設(shè)置,如下

 AllenVersionChecker
                .getInstance()
                .requestVersion()
                .setHttpHeaders(httpHeader)
                .setRequestMethod(HttpRequestMethod.POSTJSON)
                .setRequestParams(httpParam)
                .setRequestUrl(requestUrl)
                .request(new RequestVersionListener() {
                    @Nullable
                    @Override
                    public UIData onRequestVersionSuccess(String result) {
                        //拿到服務(wù)器返回的數(shù)據(jù),解析,拿到downloadUrl和一些其他的UI數(shù)據(jù)
                        ...
                        UIData uiData = UIData
                                .create()
                                .setDownloadUrl(downloadUrl)
                                .setTitle(updateTitle)
                                .setContent(updateContent);
                        //放一些其他的UI參數(shù),拿到后面自定義界面使用
                        uiData.getVersionBundle().putString("key", "your value");
                        return uiData;

                    }

                    @Override
                    public void onRequestVersionFailure(String message) {

                    }
                })
                .excuteMission(context);

以上就是最基本的使用(庫默認(rèn)會有一套界面),如果還不滿足項目需求,下面就可以用這個庫來飆車了

一些其他的function設(shè)置

解釋下,下面的builder叫DownloadBuilder

 DownloadBuilder builder=AllenVersionChecker
                .getInstance()
                .downloadOnly();
                
                
      or          
                
                
                
 DownloadBuilder builder=AllenVersionChecker
                 .getInstance()
                 .requestVersion()
                 .request()

取消任務(wù)

 AllenVersionChecker.getInstance().cancelAllMission(this);

靜默下載

 builder.setSilentDownload(true); 默認(rèn)false

強制更新

設(shè)置此listener即代表需要強制更新,會在用戶想要取消下載的時候回調(diào)
需要你自己關(guān)閉所有界面

builder.setForceUpdateListener(() -> {
              forceUpdate();
          });

下載忽略本地緩存

如果本地有安裝包緩存也會重新下載apk

 builder.setForceRedownload(true); 默認(rèn)false

是否顯示下載對話框

builder.setShowDownloadingDialog(false); 默認(rèn)true

是否顯示通知欄

builder.setShowNotification(false);  默認(rèn)true

自定義通知欄

      builder.setNotificationBuilder(
                 NotificationBuilder.create()
                         .setRingtone(true)
                         .setIcon(R.mipmap.dialog4)
                         .setTicker("custom_ticker")
                         .setContentTitle("custom title")
                         .setContentText(getString(R.string.custom_content_text))
         );

是否顯示失敗對話框

  builder.setShowDownloadFailDialog(false); 默認(rèn)true

自定義下載路徑

  builder.setDownloadAPKPath(address); 默認(rèn):/storage/emulated/0/AllenVersionPath/

可以設(shè)置下載監(jiān)聽

   builder.setApkDownloadListener(new APKDownloadListener() {
             @Override
             public void onDownloading(int progress) {
                 
             }

             @Override
             public void onDownloadSuccess(File file) {

             }

             @Override
             public void onDownloadFail() {

             }
         });

自定義界面

自定義界面使用回調(diào)方式,開發(fā)者需要返回自己定義的Dialog(父類android.app)

  • 所有自定義的界面必須使用listener里面的context實例化

  • 界面展示的數(shù)據(jù)通過UIData拿

自定義顯示更新界面

設(shè)置CustomVersionDialogListener

  • 定義此界面必須有一個確定下載的按鈕,按鈕id必須為@id/versionchecklib_version_dialog_commit

  • 如果有取消按鈕(沒有忽略本條要求),則按鈕id必須為@id/versionchecklib_version_dialog_cancel

eg.

  builder.setCustomVersionDialogListener((context, versionBundle) -> {
            BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.custom_dialog_one_layout);
            //versionBundle 就是UIData,之前開發(fā)者傳入的,在這里可以拿出UI數(shù)據(jù)并展示
            TextView textView = baseDialog.findViewById(R.id.tv_msg);
            textView.setText(versionBundle.getContent());
            return baseDialog;
        });

自定義下載中對話框界面

設(shè)置CustomDownloadingDialogListener

  • 如果此界面要設(shè)計取消操作(沒有忽略),請務(wù)必將id設(shè)置為@id/versionchecklib_loading_dialog_cancel
    builder.setCustomDownloadingDialogListener(new CustomDownloadingDialogListener() {
            @Override
            public Dialog getCustomDownloadingDialog(Context context, int progress, UIData versionBundle) {
                BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.custom_download_layout);
                return baseDialog;
            }
//下載中會不斷回調(diào)updateUI方法
            @Override
            public void updateUI(Dialog dialog, int progress, UIData versionBundle) {
                TextView tvProgress = dialog.findViewById(R.id.tv_progress);
                ProgressBar progressBar = dialog.findViewById(R.id.pb);
                progressBar.setProgress(progress);
                tvProgress.setText(getString(R.string.versionchecklib_progress, progress));
            }
        });

自定義下載失敗對話框

設(shè)置CustomDownloadFailedListener

  • 如果有重試按鈕請將id設(shè)置為@id/versionchecklib_failed_dialog_retry

  • 如果有 確認(rèn)/取消按鈕請將id設(shè)置為@id/versionchecklib_failed_dialog_cancel

   builder.setCustomDownloadFailedListener((context, versionBundle) -> {
            BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.custom_download_failed_dialog);
            return baseDialog;
        });

最后


  • 更全面的使用請看demo

  • 感謝各位對本庫的支持

  • 歡迎star/issue

License


Apache 2.0

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評論 25 709
  • 1.什么是Activity?問的不太多,說點有深度的 四大組件之一,一般的,一個用戶交互界面對應(yīng)一個activit...
    JoonyLee閱讀 5,855評論 2 51
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評論 19 139
  • Android Studio JNI流程首先在java代碼聲明本地方法 用到native關(guān)鍵字 本地方法不用去實現(xiàn)...
    MigrationUK閱讀 12,080評論 7 123
  • 童話故事原版回憶:很久以前,有一位國王,他的女兒很漂亮,小公主的金球掉進(jìn)了水池里,在青蛙的幫助下?lián)旎亓私鹎?,可公?..
    講故事的兔子小姐閱讀 1,232評論 8 14

友情鏈接更多精彩內(nèi)容