android 判断app版本更新,Android App内检测更新新版本APK

本文介绍了如何在Android应用中实现自动检测并安装新版本APK的方法。通过BroadcastReceiver监听SD卡或U盘设备接入,检测指定目录下的新版本APK,并使用后台服务SilentInstallService进行静默安装。同时,文章提到了安装过程中可能遇到的权限和签名问题及其解决方案。
摘要由CSDN通过智能技术生成

Rayland主板虽然作为一块基于Android的工控板,但是很多设备厂商并不想让用户看到Android系统信息。所以APK默认设置为开机启动项、img去除了Android头部和底部菜单。但是随之带来了APK更新的问题,传统的插入u盘,sd卡手动安装新版本APK的方式已经不够用了。所以我们需要点自动的东西。

App内检测更新新版本APK

检测新版本APK

我们使用 四大组件之一的BroadcastReceiver来检测 sd卡或是u盘设备的接入。

public class StorageMountListener extends BroadcastReceiver{

@Override

public void onReceive(final Context context, Intent intent) {

if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){

// 获取插入设备路径

String path = intent.getData().getPath();

// 检测路径是否有新版本APK

ApkUpdateUtils.getInstance().checkLocalUpdateAtBackground(context, path);

}

}

}

ApkUpdateUtils.java

/**

* 后台检查指定目录下是否有新版本的APK,有则提示安装

* @param context 上下文

* @param path 需要检查的目录

*/

public void checkLocalUpdateAtBackground(final Context context, final String path){

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

@Override

public void run() {

// 检查指定目录下是否存在高版本的更新包,并返回结果

File apkFile = findUpdatePackage(context, path);

if(apkFile == null){

return;

}

File msg = new File(apkFile.getParent(), apkFile.getName().replace(".apk", ".txt"));

String description = readStringFile(msg);

Intent intent = new Intent(context, UpdateActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// 新版本apk 路径

intent.putExtra("apk", apkFile.getAbsolutePath());

// 新版本apk 描述信息

intent.putExtra("description", descripti

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值