1.申请权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
PermissionsUtil.checkAndRequestPermissions(this);
2.FileProvider存储配置
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
xml/file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_Files" path="."/>
</paths>
3.数据请求
private void updateApp() {
String deviceId = getDeviceId();
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String token = SharedPreUtil.getInstance(mContext).getString(Constant.USER_TOKEN);
String version = getVersionName(mContext);
LogUtil.i("检测版本参数 token="+token);
http.post().url(Constant.BASE_URL + Constant.UPDATE_APP)
.addHeader("token", token)
.addHeader("app_id", Constant.APP_ID)
.addHeader("interface_version", version)
.addHeader("device_id", deviceId)
.addHeader("os", "android")
.addHeader("timestamp", timestamp)
.addHeader("Content-Type", "application/json")
.tag(this).enqueue(new JsonResponseHandler() {
@Override
public void onSuccess(int statusCode, JSONObject response) {
LogUtil.d("检测版本=" + response);
int code = response.optInt("code");
String message = response.optString("message");
String data = response.optString("data");
try {
JSONObject _data = new JSONObject(data);
if (code == 1023) {//下载新版本
updateDescription = _data.optString("appDescriptoin");
String forceUpdate = _data.optString("forceUpdate");
if (forceUpdate.equals("true")) {
isForceUpdate = 1;
} else {
isForceUpdate = 2;
}
String downloadUrl = _data.optString("downloadUrl");
if (downloadUrl.contains("\\")) {
downloadUrl = downloadUrl.replace("\\", "");
}
if (SharedPreUtil.getInstance(mContext).getInt(Constant.IS_DOWNLOAD_APP) != 1) {
downloadApp(downloadUrl);
}
} else if (code == 1024) {//版本异常
} else if (code == 200) {//最新版本
ToastUtil.show(mContext, message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, String error_msg) {
LogUtil.d("检测版本 onFailure=" + error_msg);
}
});
}
private void downloadApp(String url) {
// downloadPath = Environment.getExternalStorageDirectory() + "/Download/GollumPay/GollumPay_" + System.currentTimeMillis() + ".apk";
downloadPath = Environment.getExternalStorageDirectory() + "/Download/ChatRoom/ChatRoom_" + System.currentTimeMillis() + ".apk";
LogUtil.i("检测版本=" + url + " downloadPath=" + downloadPath);
http.download()
.url(url)
.filePath(downloadPath)
.tag(this)
.enqueue(new DownloadResponseHandler() {
@Override
public void onStart(long totalBytes) {
LogUtil.i("更新app 开始下载");
}
@Override
public void onFinish(File downloadFile) {
File file = new File(downloadPath);
SharedPreUtil.getInstance(mContext).saveParam(Constant.SAVE_DOWNLOAD_APP_PATH, downloadPath);
SharedPreUtil.getInstance(mContext).saveParam(Constant.IS_DOWNLOAD_APP, 1);
// String downloadMd5 = getFileMD5(file);
LogUtil.i("更新app 下载完成=" + isForceUpdate /*+ " downloadMd5=" + downloadMd5 + " md5=" + md5*/);
if (isForceUpdate == 1 /*&& downloadMd5.equals(md5)*/) {
updateDialog(updateDescription, file);
}
}
@Override
public void onProgress(long currentBytes, long totalBytes) {
int a = (int) ((100 * currentBytes) / totalBytes);
LogUtil.i("更新app 下载进度=" + a + "%" + totalBytes);
}
@Override
public void onFailure(String error_msg) {
LogUtil.i("更新app 下载失败");//1.外部存储的读取权限未获取
}
});
}
private void updateDialog(String updateContext, final File file) {
LogUtil.i("更新app 弹窗");
final UpdateAppDialog myDialog = new UpdateAppDialog(mContext);
myDialog.setContent(updateContext);
myDialog.show();
myDialog.setAttributes();//需先显示,然后才能查找控件
myDialog.setOnUpdateListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
installApk(file);
}
});
}
private void installApk(File file) {
if (!file.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(mContext, "${applicationId}.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
mContext.startActivity(intent);
}
private String getVersionName(Context context) {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
String versionName = "";
try {
packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
网友评论