最近业务有涉及到,奈何是个app代码小白,遂记录一下
一:AndroidManifest.xml文件配置
application标签里面加上
android:networkSecurityConfig="@xml/network_config"
<!-- app下载更新配置-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
<!-- 存储权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 安装APK权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /><!-- 将以下 <provider> 标签移动到 <application> 标签内部 -->
<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.shuye.znsy.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" />
</provider>
二:file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android"><!--安装包文件存储路径--><external-files-pathname="my_download"path="Download" /><external-pathname="."path="." />
</paths>
三:network_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config><base-config cleartextTrafficPermitted="true" />
</network-security-config>
如果没有这两个文件需要新建然后将以上美容复制进去
四:progress.xml滚动条布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/txtStatus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="状态"android:textSize="10sp"android:textStyle="normal" /><ProgressBarandroid:id="@+id/progress"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_toLeftOf="@id/txtStatus" /></LinearLayout>
</LinearLayout>
五:新建AutoUpdater.java 这里是核心
package com.shuye.znsy.update;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;import androidx.core.content.FileProvider;import com.shuye.znsy.BuildConfig;
import com.shuye.znsy.R;import org.json.JSONException;
import org.json.JSONObject;import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;public class AutoUpdater {// 下载安装包的网络路径private String apkUrl = BuildConfig.host + "app/upload/download-apk";protected String checkUrl = apkUrl + "/output-metadata-json";// 保存APK的文件名private static final String saveFileName = "my.apk";private static File apkFile;// 下载线程private Thread downLoadThread;private int progress;// 当前进度// 应用程序Contextprivate Context mContext;// 是否是最新的应用,默认为falseprivate boolean isNew = false;private boolean intercept = false;// 进度条与通知UI刷新的handler和msg常量private ProgressBar mProgress;private TextView txtStatus;private static final int DOWN_UPDATE = 1;private static final int DOWN_OVER = 2;private static final int SHOWDOWN = 3;public AutoUpdater(Context context) {mContext = context;apkFile = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), saveFileName);}public void ShowUpdateDialog() {AlertDialog.Builder builder = new AlertDialog.Builder(mContext);builder.setTitle("软件版本更新");builder.setMessage("有最新的软件包,请下载并安装!");builder.setPositiveButton("立即下载", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ShowDownloadDialog();}});//按钮隐藏,只能下载更新
// builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// }
// });AlertDialog dialog = builder.create();dialog.setOnShowListener(new DialogInterface.OnShowListener() {@Overridepublic void onShow(DialogInterface dialogInterface) {Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);if (btnPos != null) {btnPos.setTextColor(Color.WHITE);}if (btnNeg != null) {btnNeg.setTextColor(Color.WHITE);}}});// 设置点击空白区域不关闭对话框dialog.setCanceledOnTouchOutside(false);//这里设置点击返回按钮对话框不消失dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {@Overridepublic boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {if (keyCode == KeyEvent.KEYCODE_BACK) {// 拦截返回按钮事件,不做任何操作return true;}return false;}});dialog.show();}private void ShowDownloadDialog() {AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);dialogBuilder.setTitle("软件版本更新");LayoutInflater inflater = LayoutInflater.from(mContext);View v = inflater.inflate(R.layout.progress, null);mProgress = (ProgressBar) v.findViewById(R.id.progress);txtStatus = v.findViewById(R.id.txtStatus);dialogBuilder.setView(v);dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {intercept = true;}});AlertDialog dialog = dialogBuilder.create();dialog.setOnShowListener(new DialogInterface.OnShowListener() {@Overridepublic void onShow(DialogInterface dialogInterface) {Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);if (btnNeg != null) {btnNeg.setTextColor(Color.WHITE);}}});// 设置点击空白区域不关闭对话框dialog.setCanceledOnTouchOutside(false);//这里设置点击返回按钮对话框不消失dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {@Overridepublic boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {if (keyCode == KeyEvent.KEYCODE_BACK) {// 拦截返回按钮事件,不做任何操作return true;}return false;}});dialog.show();DownloadApk();}/*** 检查是否更新的内容*/public void CheckUpdate() {new Thread(new Runnable() {@Overridepublic void run() {try {String localVersion = "1";try {localVersion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}String versionName = "1";String outputFile = "";String config = doGet(checkUrl);if (config != null && config.length() > 0) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {Matcher m = Pattern.compile("\"outputFile\":\\s*\"(?<m>[^\"]*?)\"").matcher(config);if (m.find()) {outputFile = m.group("m");}m = Pattern.compile("\"versionName\":\\s*\"(?<m>[^\"]*?)\"").matcher(config);if (m.find()) {String v = m.group("m");versionName = m.group("m").replace("v1.0.", "");}}}if (localVersion.contains(".")) {localVersion = localVersion.substring(0, localVersion.indexOf("."));}if (versionName.contains(".")) {versionName = versionName.substring(0, versionName.indexOf("."));}//测试环境 大于号 正式上线要改成小于号,<<<<<<<<<<<<<<<<<<<<<<if (Long.parseLong(localVersion) > Long.parseLong(versionName)) {
// apkUrl = apkUrl + outputFile;mHandler.post(new Runnable() {@Overridepublic void run() {System.out.println("版本升级开始自动更新--------------------" + apkUrl);mHandler.sendEmptyMessage(SHOWDOWN);}});} else {// 当前版本已是最新版本return;}} catch (Exception e) {e.printStackTrace();}}}).start();}/*** 从服务器下载APK安装包*/public void DownloadApk() {downLoadThread = new Thread(DownApkWork);downLoadThread.start();}private Runnable DownApkWork = new Runnable() {@Overridepublic void run() {URL url;try {url = new URL(apkUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.connect();int length = conn.getContentLength();InputStream ins = conn.getInputStream();FileOutputStream fos = new FileOutputStream(apkFile);int count = 0;byte[] buf = new byte[1024];while (!intercept) {int numread = ins.read(buf);count += numread;progress = (int) (((float) count / length) * 100);// 下载进度mHandler.sendEmptyMessage(DOWN_UPDATE);if (numread <= 0) {// 下载完成通知安装mHandler.sendEmptyMessage(DOWN_OVER);break;}fos.write(buf, 0, numread);}fos.close();ins.close();} catch (Exception e) {e.printStackTrace();}}};/*** 安装APK内容*/public void installAPK() {try {if (!apkFile.exists()) {return;}Intent intent = new Intent(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//安装完成后打开新版本intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 给目标应用一个临时授权if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0//如果SDK版本>=24,即:Build.VERSION.SDK_INT >= 24,使用FileProvider兼容安装apkString packageName = mContext.getApplicationContext().getPackageName();String authority = new StringBuilder(packageName).append(".fileprovider").toString();Uri apkUri = FileProvider.getUriForFile(mContext, authority, apkFile);intent.setDataAndType(apkUri, "application/vnd.android.package-archive");} else {intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");}// 添加权限请求intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);mContext.startActivity(intent);android.os.Process.killProcess(android.os.Process.myPid());//安装完之后会提示”完成” “打开”。} catch (Exception e) {e.printStackTrace();// 添加异常处理,打印异常信息或者显示Toast提示Toast.makeText(mContext, "安装APK出错", Toast.LENGTH_SHORT).show();}}private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case SHOWDOWN:ShowUpdateDialog();break;case DOWN_UPDATE:txtStatus.setText(progress + "%");mProgress.setProgress(progress);break;case DOWN_OVER:Toast.makeText(mContext, "下载完毕", Toast.LENGTH_SHORT).show();installAPK();break;default:break;}}};public static String doGet(String httpurl) {HttpURLConnection connection = null;InputStream is = null;BufferedReader br = null;String result = null;try {URL url = new URL(httpurl);connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(15000);connection.setReadTimeout(60000);connection.connect();if (connection.getResponseCode() == 200) {is = connection.getInputStream();br = new BufferedReader(new InputStreamReader(is, "UTF-8"));StringBuffer sbf = new StringBuffer();String temp = null;while ((temp = br.readLine()) != null) {sbf.append(temp);sbf.append("\r\n");}result = sbf.toString();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (null != br) {try {br.close();} catch (IOException e) {e.printStackTrace();}}if (null != is) {try {is.close();} catch (IOException e) {e.printStackTrace();}}connection.disconnect();}return result;}
}
六:MainActivity.java
/*** app检查更新*/private void appUpdata(){//检查更新try {//6.0才用动态权限if (Build.VERSION.SDK_INT >= 23) {String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.INTERNET};List<String> permissionList = new ArrayList<>();for (int i = 0; i < permissions.length; i++) {if (ActivityCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {permissionList.add(permissions[i]);}}if (permissionList.size() <= 0) {//说明权限都已经通过,可以做你想做的事情去//自动更新AutoUpdater manager = new AutoUpdater(MainActivity.this);manager.CheckUpdate();} else {//存在未允许的权限ActivityCompat.requestPermissions(this, permissions, 100);}}} catch (Exception ex) {Toast.makeText(MainActivity.this, "自动更新异常:" + ex.getMessage(), Toast.LENGTH_SHORT).show();}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);boolean haspermission = false;if (100 == requestCode) {for (int i = 0; i < grantResults.length; i++) {if (grantResults[i] == -1) {haspermission = true;}}if (haspermission) {//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问permissionDialog();} else {//全部权限通过,可以进行下一步操作AutoUpdater manager = new AutoUpdater(MainActivity.this);manager.CheckUpdate();}}}AlertDialog alertDialog;//打开手动设置应用权限private void permissionDialog() {if (alertDialog == null) {alertDialog = new AlertDialog.Builder(this).setTitle("提示信息").setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。").setPositiveButton("设置", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {cancelPermissionDialog();Uri packageURI = Uri.parse("package:" + getPackageName());Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);startActivity(intent);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {cancelPermissionDialog();}}).create();}alertDialog.show();}private void cancelPermissionDialog() {alertDialog.cancel();}private static final long HEARTBEAT_INTERVAL = 60 * 60 * 1000; // 1小时的毫秒数
// private static final long HEARTBEAT_INTERVAL = 1 * 60 * 1000; // 每隔一分钟的毫秒数private Handler heartbeatHandler = new Handler();private Runnable heartbeatRunnable = new Runnable() {@Overridepublic void run() {appUpdata(); // 调用appUpdata()方法heartbeatHandler.postDelayed(this, HEARTBEAT_INTERVAL); // 每隔1小时再次执行}};private void startHeartbeat() {heartbeatHandler.postDelayed(heartbeatRunnable, HEARTBEAT_INTERVAL);}private void stopHeartbeat() {heartbeatHandler.removeCallbacks(heartbeatRunnable);}}
然后在creat里面调用心跳
@Overrideprotected void onCreate(Bundle savedInstanceState) {//其他代码省略 startHeartbeat();}
这里要说一下,打包的时候可以根据当前时间设置,修改build.gradle文件
plugins {id 'com.android.application'//添加如下配置id 'com.huawei.agconnect'
}
apply plugin: 'com.huawei.agconnect'android {compileSdk 31defaultConfig {applicationId "com.shuye.znsy"minSdk 21targetSdk 31versionCode 1
// versionName "1.0"versionName "${releaseTime()}"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {debug {signingConfig signingConfigs.debugdebuggable trueminifyEnabled falsebuildConfigField("String", "host", "\"http://192.168.100.111:8001/\"")proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'android.applicationVariants.all { variant ->variant.outputs.all {outputFileName = "my_${releaseTime()}.apk"}}}release {minifyEnabled falseshrinkResources falsezipAlignEnabled truejniDebuggable falsedebuggable falsebuildConfigField("String", "host", "\"http://192.168.100.111:8001/\"")proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'android.applicationVariants.all { variant ->variant.outputs.all {outputFileName = "my_${releaseTime()}.apk"}}}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation 'androidx.appcompat:appcompat:1.3.0'implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
// implementation 'androidx.media3:media3-common:+'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'implementation 'com.github.Jasonchenlijian:FastBle:2.4.0'implementation 'com.huawei.hms:scanplus:2.12.0.301'//数据解析相关implementation 'com.alibaba:fastjson:1.2.37'implementation 'io.reactivex:rxjava:1.3.0'implementation 'io.reactivex:rxandroid:1.2.1'implementation 'com.google.code.gson:gson:2.8.6'implementation 'com.orhanobut:logger:2.1.1'//网络框架implementation 'com.squareup.retrofit2:retrofit:2.4.0'implementation 'com.squareup.retrofit2:converter-gson:2.4.0'implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'implementation 'io.reactivex.rxjava2:rxjava:2.1.16'implementation 'com.squareup.okhttp3:okhttp:3.11.0'implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'//eventbusimplementation 'org.greenrobot:eventbus:3.0.0'}
def releaseTime() {return new Date().format("yyyyMMddHHmmss", TimeZone.getTimeZone("UTC"))
}
打包后得到的文件
以上是app内所要修改或添加全部代码,注意这里需要配合后端下载文件联合使用,考虑到这里遂上一下后端代码,每个人情况不一样,仅供参考
我这里是springboot框架,java语言
一个为下载文件apk的方法,另外一个读取output-metadata.json反馈到app进行版本解析比较
注意这里我后端已经有了文件上传的方法,我是将apk和output-metadata.json一起上传到了服务器上面
package com.cz.hospital.base.controller.pad;import com.cz.hospital.base.service.IPackageUploadService;
import com.cz.hospital.base.service.IUserService;
import com.cz.hospital.entity.attachment.AttachmentEntity;
import com.cz.hospital.entity.packageupload.vo.PackageUploadVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;@Slf4j
@RestController
@RequestMapping("/app/upload")
@Api(tags = "app安装包下载管理接口")
public class AppUpdateController {@Resourceprivate IPackageUploadService packageUploadService;@Resourceprivate IUserService userService;@RequestMapping("/download-apk")public ResponseEntity<org.springframework.core.io.Resource> downloadNewestFile() throws IOException {PackageUploadVo vo = packageUploadService.findNewestList();List<AttachmentEntity> attachmentList = vo.getAttachmentList();AttachmentEntity file = new AttachmentEntity();if( attachmentList.size()>0 ){if( attachmentList.size()>0 ){for( AttachmentEntity aeF : attachmentList ){if( ".apk".equals( aeF.getFileExtension() ) ){file = aeF;}}}}String filePath = file.getFileStoragePath();String fileName = file.getFileName();// 创建文件资源对象Path path = Paths.get(filePath);org.springframework.core.io.Resource resource = new UrlResource(path.toUri());// 设置响应头HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));headers.set(HttpHeaders.CONTENT_ENCODING, "UTF-8");headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));// 返回文件内容return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength()).contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);}@RequestMapping("/download-apk/output-metadata-json")public String readJson() throws IOException {PackageUploadVo vo = packageUploadService.findNewestList();List<AttachmentEntity> attachmentList = vo.getAttachmentList();AttachmentEntity file = new AttachmentEntity();if( attachmentList.size()>0 ){if( attachmentList.size()>0 ){for( AttachmentEntity aeF : attachmentList ){if( ".json".equals( aeF.getFileExtension() ) ){file = aeF;}}}}// 读取output-metadata.json文件String filePath = file.getFileStoragePath();String fileName = file.getFileName();// 创建文件资源对象Path path = Paths.get(filePath);org.springframework.core.io.Resource resource = new UrlResource(path.toUri());InputStream inputStream = resource.getInputStream();byte[] jsonBytes = FileCopyUtils.copyToByteArray(inputStream);String jsonContent = new String(jsonBytes, StandardCharsets.UTF_8);// 返回文件内容return jsonContent;}}
至此结束,有问题欢迎大家随时交流