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