当前位置: 首页 > news >正文

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拿到手软!

相关文章:

  • DP动态规划(下)
  • 【产品经理】订单处理8-智能分仓
  • 面向对象的程序设计:对象数组,对象指针书后习题——第九章(P295)第九题
  • SpringBoot配置第三方专业缓存框架j2cache
  • 游戏心理学Day18
  • Ps:脚本与动作
  • miniconda安装教程以及pip换源【Windows版本】
  • 删除名为 `XXXX` 的 conda 环境的命令
  • LLM 理论知识
  • Linux中的进程控制
  • 农业领域科技查新点提炼方法附案例!
  • uniapp原生插件开发实战——Android打开文件到自己的app
  • 数据库设计规范总结
  • windows 下 docker 入门
  • JWT详解、JWTUtil工具类的构建方法
  • [数据结构]链表的实现在PHP中
  • ES6之路之模块详解
  • github从入门到放弃(1)
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • js对象的深浅拷贝
  • leetcode46 Permutation 排列组合
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • SwizzleMethod 黑魔法
  • Vue 2.3、2.4 知识点小结
  • 成为一名优秀的Developer的书单
  • 浅谈Golang中select的用法
  • 嵌入式文件系统
  • 深度解析利用ES6进行Promise封装总结
  • 为什么要用IPython/Jupyter?
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 想写好前端,先练好内功
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 译米田引理
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • 阿里云服务器如何修改远程端口?
  • 阿里云重庆大学大数据训练营落地分享
  • 容器镜像
  • #1014 : Trie树
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #Ubuntu(修改root信息)
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (2024)docker-compose实战 (8)部署LAMP项目(最终版)
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (附源码)php新闻发布平台 毕业设计 141646
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转)重识new
  • .bat批处理(一):@echo off
  • .bat批处理出现中文乱码的情况