注册

功能强大的升级库

CheckVersionLib


V2版震撼来袭,功能强大,链式编程,调用简单,集成轻松,扩展性强大

老规矩先看V2效果,这个版本最大的特点就是使用非常简单,相对于1.+版本


效果

V2.gif



特点



  • 任何地方都可以调用




  • 简单简单简单简单(重要的话我说四遍)




  • 扩展性强大




  • 所有具有升级功能的app均可使用,耶稣说的




  • 更强大的自定义界面支持




  • 支持强制更新(一行代码)




  • 支持静默下载 (一行代码)




  • 适配到Android Q




导入

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}


appcompat

  implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.4.1_appcompat'


jitpack && android x

dependencies {
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.4.1_androidx'
}


使用

和1.+版本一样,两种模式


只使用下载模式


先来个最简单的调用

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

UIData:UIData是一个Bundle,用于存放用于UI展示的一些数据,后面自定义界面时候可以拿来用


请求服务器版本+下载


该模式最简单的使用

   AllenVersionChecker
.getInstance()
.requestVersion()
.setRequestUrl(requestUrl)
.request(new RequestVersionListener() {
@Nullable
@Override
public UIData onRequestVersionSuccess(String result) {
//拿到服务器返回的数据,解析,拿到downloadUrl和一些其他的UI数据
...
//如果是最新版本直接return null
return UIData.create().setDownloadUrl(downloadUrl);
}

@Override
public void onRequestVersionFailure(String message) {

}
})
.executeMission(context);

请求版本一些其他的http参数可以设置,如下

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

}

@Override
public void onRequestVersionFailure(String message) {

}
})
.executeMission(context);


合适的地方关闭任务

为了避免不必要的内存泄漏,需要在合适的地方取消任务

AllenVersionChecker.getInstance().cancelAllMission();

以上就是最基本的使用(库默认会有一套界面),如果还不满足项目需求,下面就可以用这个库来飙车了


一些其他的function设置

解释下,下面的builder叫DownloadBuilder

 DownloadBuilder builder=AllenVersionChecker
.getInstance()
.downloadOnly();


or



DownloadBuilder builder=AllenVersionChecker
.getInstance()
.requestVersion()
.request()

取消任务


 AllenVersionChecker.getInstance().cancelAllMission(this);

静默下载


 builder.setSilentDownload(true); 默认false

设置当前服务器最新的版本号,供库判断是否使用缓存



  • 缓存策略:如果本地有安装包,首先判断与当前运行的程序的versionCode是否不一致,然后判断是否有传入最新的
    versionCode,如果传入的versionCode大于本地的,重新从服务器下载,否则使用缓存
 builder.setNewestVersionCode(int); 默认null

强制更新


设置此listener即代表需要强制更新,会在用户想要取消下载的时候回调
需要你自己关闭所有界面

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

update in v2.2.1
动态设置是否强制更新,如果使用本库来请求服务器,可以在回调时动态设置一些参数或者回调

   public UIData onRequestVersionSuccess(DownloadBuilder downloadBuilder,String result) {
downloadBuilder.setForceUpdateListener(() -> {
forceUpdate();
});
Toast.makeText(V2Activity.this, "request successful", Toast.LENGTH_SHORT).show();
return crateUIData();
}

下载忽略本地缓存


如果本地有安装包缓存也会重新下载apk

 builder.setForceRedownload(true); 默认false

是否显示下载对话框


builder.setShowDownloadingDialog(false); 默认true

是否显示通知栏


builder.setShowNotification(false);  默认true

以前台service运行(update in 2.2.2)
推荐以前台服务运行更新,防止在后台时,服务被杀死


builder.setRunOnForegroundService(true); 默认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); 默认true

自定义下载路径


  builder.setDownloadAPKPath(address); 默认:/storage/emulated/0/AllenVersionPath/

自定义下载文件名


  builder.setApkName(apkName); 默认:getPackageName()

可以设置下载监听


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

}

@Override
public void onDownloadSuccess(File file) {

}

@Override
public void onDownloadFail() {

}
});

设置取消监听
此回调会监听所有cancel事件


 
builder.setOnCancelListener(() -> {
Toast.makeText(V2Activity.this,"Cancel Hanlde",Toast.LENGTH_SHORT).show();
});

如果想单独监听几种状态下的cancel,可像如下这样设置


  • builder.setDownloadingCancelListener();
  • builder.setDownloadFailedCancelListener();
  • builder.setReadyDownloadCancelListener();

设置确定监听(added after 2.2.2)



  • builder.setReadyDownloadCommitClickListener();
  • builder.setDownloadFailedCommitClickListener();

静默下载+直接安装(不会弹出升级对话框)


    builder.setDirectDownload(true);
builder.setShowNotification(false);
builder.setShowDownloadingDialog(false);
builder.setShowDownloadFailDialog(false);

自定义安装回调


    setCustomDownloadInstallListener(CustomInstallListener customDownloadInstallListener)


自定义界面

自定义界面使用回调方式,开发者需要返回自己定义的Dialog(父类android.app)



  • 所有自定义的界面必须使用listener里面的context实例化




  • 界面展示的数据通过UIData拿




自定义显示更新界面


设置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,之前开发者传入的,在这里可以拿出UI数据并展示
TextView textView = baseDialog.findViewById(R.id.tv_msg);
textView.setText(versionBundle.getContent());
return baseDialog;
});

自定义下载中对话框界面


设置CustomDownloadingDialogListener


  • 如果此界面要设计取消操作(没有忽略),请务必将id设置为@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;
}
//下载中会不断回调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));
}
});

自定义下载失败对话框


设置CustomDownloadFailedListener



  • 如果有重试按钮请将id设置为@id/versionchecklib_failed_dialog_retry




  • 如果有 确认/取消按钮请将id设置为@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;
});


update Log


  • 2.2.1

    • 修复内存泄漏问题
    • 使用binder传递参数
    • 一些已知的bug



混淆配置

 -keepattributes *Annotation*
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe ;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
(java.lang.Throwable);
}

git地址:https://github.com/AlexLiuSheng/CheckVersionLib

下载地址:CheckVersionLib-master.zip

0 个评论

要回复文章请先登录注册