写点什么

Android-app 自动更新总结(已适配 9-0),一个回答引发热烈讨论

用户头像
Android架构
关注
发布于: 刚刚

<meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths"?/></provider>

1.2 新建文件(路径:res\xml\file_paths.xml):

<paths><external-path?path="."?name="external_storage_root"?/></paths>

1.3 (app 的)build.gradle:

implementation?"com.lzy.net:okgo:3.0.4"//okgo?网络请求 implementation?'com.google.code.gson:gson:2.8.2'//gsonimplementation?"org.permissionsdispatcher:permissionsdispatcher:4.3.1"//权限 annotationProcessor?"org.permissionsdispatcher:permissionsdispatcher-processor:4.3.1"//权限

2.这里以点击按钮进行更新为例:
2.1 核心代码:

private?int?version;/?更新进度条?/private?ProgressBar?mProgress;private?AlertDialog?mDownloadDialog;




//点击按钮,检查权限,,,检查更新的方法 @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.REQUEST_INSTALL_PACKAGES})protected?void?checkUpdate()?{showLoadingDialog("检测更新中...");version?=?AppUpdateUtil.getAppVersionCode(this);//检查当前版本号//??????调用方法,,,接口的具体实现,接收传过来的参数,再调自己的方法,requestAppUpdate(version,?new?DataRequestListener<UpdateAppBean>()?{@Overridepublic?void?success(UpdateAppBean?data)?{//????????????????返回的 json,getStatus 为 0 时,去下载 apk 文件,这里是下载 apk 文件的方法 updateApp(data.getData().getApk_url());}


@Overridepublic?void?fail(String?msg)?{//????????????????返回的 json,getStatus 为 1 时,提示:"已是最新版本!"SToast(msg);dismissLoadingDialog();}});}


//检查版本号,第一次请求(post),,,UpdateAppBean 根据服务器返回生成 private?void?requestAppUpdate(int?version,?final?DataRequestListener<UpdateAppBean>?listener)?{OkGo.<String>post(Const.HOST_URL?+?Const.UPDATEAPP).params("version",?version).execute(new?StringCallback()?{@Overridepublic?void?onSuccess(Response<String>?response)?{Gson?gson?=?new?Gson();UpdateAppBean?updateAppBean?=?gson.fromJson(response.body(),?UpdateAppBean.class);if?(updateAppBean.getStatus()?==?0)?{listener.success(updateAppBean);}?else?{listener.fail(updateAppBean.getMsg());}}


@Overridepublic?void?onError(Response<String>?response)?{listener.fail("服务器连接失败");dismissLoadingDialog();}});}


//如果有新版本,提示有新的版本,然后下载 apk 文件 private?void?updateApp(String?apk_url)?{dismissLoadingDialog();DialogUtils.getInstance().showDialog(this,?"发现新的版本,是否下载更新?",new?DialogUtils.DialogListener()?{@Overridepublic?void?positiveButton()?{downloadApp(apk_url);}});}


//下载 apk 文件并跳转(第二次请求,get)private?void?downloadApp(String?apk_url)?{OkGo.<File>get(apk_url).tag(this).execute(new?FileCallback()?{@Overridepublic?void?onSuccess(Response<File>?response)?{String?filePath?=?response.b


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ody().getAbsolutePath();Intent?intent?=?IntentUtil.getInstallAppIntent(mContext,?filePath);//????????????????测试过这里必须用 startactivity,不能用 stratactivityforresultmContext.startActivity(intent);dismissLoadingDialog();mDownloadDialog.dismiss();mDownloadDialog=null;}


@Overridepublic?void?downloadProgress(Progress?progress)?{//????????????????????????showDownloadDialog();//????????????????????????mProgress.setProgress((int)?(progress.fraction??100));if?(mDownloadDialog?==?null)?{//?构造软件下载对话框 AlertDialog.Builder?builder?=?new?AlertDialog.Builder(mContext);builder.setTitle("正在更新");//?给下载对话框增加进度条 final?LayoutInflater?inflater?=?LayoutInflater.from(mContext);View?v?=?inflater.inflate(R.layout.item_progress,?null);mProgress?=?(ProgressBar)?v.findViewById(R.id.update_progress);builder.setView(v);mDownloadDialog?=?builder.create();mDownloadDialog.setCancelable(false);mDownloadDialog.show();}mProgress.setProgress((int)?(progress.fraction??100));}});}

2.2 DataRequestListener:

public?interface?DataRequestListener<T>?{//请求成功 void?success(T?data);//请求失败 void?fail(String?msg);}


接下来是工具类,来自 github,参考,https://github.com/vondear/RxTool

2.3 AppUpdateUtil:
用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android-app自动更新总结(已适配9-0),一个回答引发热烈讨论