-
-
Notifications
You must be signed in to change notification settings - Fork 413
高阶使用
xuexiangjys edited this page Mar 21, 2019
·
6 revisions
可设置弹窗的标题背景和按钮颜色。
- themeColor: 设置主题颜色(升级/安装按钮的背景色)
- topResId: 弹窗的标题背景的资源图片
XUpdate.newBuild(getActivity())
.updateUrl(mUpdateUrl)
.themeColor(ResUtils.getColor(R.color.update_theme_color))
.topResId(R.mipmap.bg_update_top)
.update();
实现IUpdateParser接口即可实现解析器的自定义。
XUpdate.newBuild(getActivity())
.updateUrl(mUpdateUrl3)
.updateParser(new CustomUpdateParser()) //设置自定义的版本更新解析器
.update();
public class CustomUpdateParser implements IUpdateParser {
@Override
public UpdateEntity parseJson(String json) throws Exception {
CustomResult result = JsonUtil.fromJson(json, CustomResult.class);
if (result != null) {
return new UpdateEntity()
.setHasUpdate(result.hasUpdate)
.setIsIgnorable(result.isIgnorable)
.setVersionCode(result.versionCode)
.setVersionName(result.versionName)
.setUpdateContent(result.updateLog)
.setDownloadUrl(result.apkUrl)
.setSize(result.apkSize);
}
return null;
}
}
-
实现
IUpdateChecker接口即可实现检查器的自定义。 -
实现
IUpdateParser接口即可实现解析器的自定义。 -
实现
IUpdatePrompter接口即可实现提示器的自定义。
XUpdate.newBuild(getActivity())
.updateUrl(mUpdateUrl3)
.updateChecker(new DefaultUpdateChecker() {
@Override
public void onBeforeCheck() {
super.onBeforeCheck();
CProgressDialogUtils.showProgressDialog(getActivity(), "查询中...");
}
@Override
public void onAfterCheck() {
super.onAfterCheck();
CProgressDialogUtils.cancelProgressDialog(getActivity());
}
})
.updateParser(new CustomUpdateParser())
.updatePrompter(new CustomUpdatePrompter(getActivity()))
.update();
public class CustomUpdatePrompter implements IUpdatePrompter {
private Context mContext;
public CustomUpdatePrompter(Context context) {
mContext = context;
}
@Override
public void showPrompt(@NonNull UpdateEntity updateEntity, @NonNull IUpdateProxy updateProxy, @NonNull PromptEntity promptEntity) {
showUpdatePrompt(updateEntity, updateProxy);
}
/**
* 显示自定义提示
*
* @param updateEntity
* @param updateProxy
*/
private void showUpdatePrompt(final @NonNull UpdateEntity updateEntity, final @NonNull IUpdateProxy updateProxy) {
String updateInfo = UpdateUtils.getDisplayUpdateInfo(mContext, updateEntity);
new AlertDialog.Builder(mContext)
.setTitle(String.format("是否升级到%s版本?", updateEntity.getVersionName()))
.setMessage(updateInfo)
.setPositiveButton("升级", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
updateProxy.startDownload(updateEntity, new OnFileDownloadListener() {
@Override
public void onStart() {
HProgressDialogUtils.showHorizontalProgressDialog(mContext, "下载进度", false);
}
@Override
public void onProgress(float progress, long total) {
HProgressDialogUtils.setProgress(Math.round(progress * 100));
}
@Override
public boolean onCompleted(File file) {
HProgressDialogUtils.cancel();
return true;
}
@Override
public void onError(Throwable throwable) {
HProgressDialogUtils.cancel();
}
});
}
})
.setNegativeButton("暂不升级", null)
.setCancelable(false)
.create()
.show();
}
XUpdate.newBuild(getActivity())
.apkCacheDir(PathUtils.getExtDownloadsPath()) //设置下载缓存的根目录
.build()
.download(mDownloadUrl, new OnFileDownloadListener() { //设置下载的地址和下载的监听
@Override
public void onStart() {
HProgressDialogUtils.showHorizontalProgressDialog(getContext(), "下载进度", false);
}
@Override
public void onProgress(float progress, long total) {
HProgressDialogUtils.setProgress(Math.round(progress * 100));
}
@Override
public boolean onCompleted(File file) {
HProgressDialogUtils.cancel();
ToastUtils.toast("apk下载完毕,文件路径:" + file.getPath());
return false;
}
@Override
public void onError(Throwable throwable) {
HProgressDialogUtils.cancel();
}
});
_XUpdate.startInstallApk(getContext(), FileUtils.getFileByPath(PathUtils.getFilePathByUri(getContext(), data.getData()))); //填写文件所在的路径
如果你的apk安装与众不同,你可以实现自己的apk安装器。你只需要实现OnInstallListener接口,并通过XUpdate.setOnInstallListener进行设置即可生效。