Android-app自动更新总结(已适配9-0)(1)

}

//检查版本号,第一次请求(post),,,UpdateAppBean根据服务器返回生成

private void requestAppUpdate(int version, final DataRequestListener listener) {

OkGo.post(Const.HOST_URL + Const.UPDATEAPP).params("version", version).execute(new StringCallback() {

@Override

public void onSuccess(Response 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());

}

}

@Override

public void onError(Response response) {

listener.fail("服务器连接失败");

dismissLoadingDialog();

}

});

}

//如果有新版本,提示有新的版本,然后下载apk文件

private void updateApp(String apk_url) {

dismissLoadingDialog();

DialogUtils.getInstance().showDialog(this, "发现新的版本,是否下载更新?",

new DialogUtils.DialogListener() {

@Override

public void positiveButton() {

downloadApp(apk_url);

}

});

}

//下载apk文件并跳转(第二次请求,get)

private void downloadApp(String apk_url) {

OkGo.get(apk_url).tag(this).execute(new FileCallback() {

@Override

public void onSuccess(Response response) {

String filePath = response.body().getAbsolutePath();

Intent intent = IntentUtil.getInstallAppIntent(mContext, filePath);

// 测试过这里必须用startactivity,不能用stratactivityforresult

mContext.startActivity(intent);

dismissLoadingDialog();

mDownloadDialog.dismiss();

mDownloadDialog=null;

}

@Override

public 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 {

//请求成功

void success(T data);

//请求失败

void fail(String msg);

}

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

2.3 AppUpdateUtil:

/**

* 获取App版本码

*

* @param context 上下文

* @return App版本码

*/

public static int getAppVersionCode(Context context) {

return getAppVersionCode(context, context.getPackageName());

}

2.4 IntentUtil:

public class IntentUtil {

/**

* 获取安装App(支持7.0)的意图

*

* @param context

* @param filePath

* @return

*/

public static Intent getInstallAppIntent(Context context, String filePath) {

//apk文件的本地路径

File apkfile = new File(filePath);

if (!apkfile.exists()) {

return null;

}

Intent intent = new Intent(Intent.ACTION_VIEW);

Uri contentUri = FileUtil.getUriForFile(context, apkfile);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

}

intent.setDataAndType(contentUri, "

application/vnd.android.package-archive");

return intent;

}

2.5 FileUtil:

/**

* 将文件转换成uri(支持7.0)

*

* @param mContext

* @param file

* @return

*/

public static Uri getUriForFile(Context mContext, File file) {

Uri fileUri = null;

if (Build.VERSION.SDK_INT >= 24) {

fileUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", file);

} else {

fileUri = Uri.fromFile(file);

}

return fileUri;

}

3.遇到的问题

9.0手机authorities配置出错,导致无法安装,

解决办法:

1.项目中使用了Androidx,AndroidManifest.xml的配置中就必须使用androidx的fileprovider

2.这里的authorities与FileUtil.java中的要一样,我就是字母P大写了导致错误

金三银四马上就要过去了,还没有找到工作的伙伴推荐一篇文章:
"寒冬未过",阿里P9架构分享Android必备技术点,让你offer拿到手软!

Uzg-1718560595077)]

金三银四马上就要过去了,还没有找到工作的伙伴推荐一篇文章:
"寒冬未过",阿里P9架构分享Android必备技术点,让你offer拿到手软!
相关推荐
私人珍藏库9 小时前
【Android】Soul v5.86.0 内置模块版
android·app·工具·软件·多功能
千里马学框架9 小时前
aosp新增窗口层级 Type 完整实现方案(有源码)-wms需求和面试题
android·智能手机·架构·wms·aaos·车机
峥嵘life15 小时前
Android 蓝牙设备连接广播详解-2026
android·python·学习
MusingByte18 小时前
别再裸用 Claude Code 了!安卓开发者必装 13 个官方推荐插件,效率翻 3 倍省 70% token
android
_李小白18 小时前
【android opencv学习笔记】Day 29: 滤波算法之Sobel 边缘检测
android·opencv·学习
Dxy123931021619 小时前
Python 操作 MySQL 事务:从入门到避坑
android·python·mysql
峥嵘life20 小时前
Android getprop 属性限制详解:User 版本属性获取问题分析
android·开发语言·python·学习
一航jason21 小时前
Speed Tools:一套低侵入的 Android 插件化 + 动态换肤 + 字体切换框架
android·插件化·组件化·换肤
李斯维1 天前
Jetpack 可观察数据容器 LiveData 的入门与基础使用
android·android jetpack
问心无愧05131 天前
ctf show web入门261
android·前端·笔记