修复:修改更新Apk逻辑,(雷电模拟器卡死问题)
This commit is contained in:
parent
269f6c64a9
commit
488cc192b2
@ -189,8 +189,5 @@ dependencies {
|
||||
api files('libs/Msc.jar')
|
||||
|
||||
api 'com.github.li-xiaojun:XPopup:2.9.1'
|
||||
//app-updater
|
||||
api 'com.github.jenly1314.AppUpdater:app-updater:1.1.3'
|
||||
//app-dialog
|
||||
api 'com.github.jenly1314.AppUpdater:app-dialog:1.1.3'
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,184 @@
|
||||
package com.yunbao.common.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.Display;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import com.yunbao.common.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class APKDownloadUtil {
|
||||
/**
|
||||
* APK文件下载
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void downloadAPK(Activity context, String url, OnUpdateListener listener) {
|
||||
Request request = new Request.Builder().url(url)
|
||||
.addHeader("Accept-Encoding", "identity").build();
|
||||
File downloadFile = new File(context.getCacheDir(), "update_app.apk");
|
||||
try {
|
||||
if (!downloadFile.exists()
|
||||
&& !downloadFile.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
showDialod(context);
|
||||
new OkHttpClient().newCall(request).enqueue(new Callback() {
|
||||
private Handler handler = new Handler();
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
// 下载失败
|
||||
handler.post(() -> {
|
||||
listener.updateFailure(-1, e.getMessage());
|
||||
progressDialog.dismiss();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) {
|
||||
Looper.prepare();
|
||||
byte[] buf = new byte[2048];
|
||||
int len;
|
||||
try (InputStream inputStream = response.body().byteStream();
|
||||
FileOutputStream outputStream = new FileOutputStream(downloadFile)) {
|
||||
long total = response.body().contentLength();
|
||||
long sum = 0;
|
||||
while ((len = inputStream.read(buf)) != -1) {
|
||||
outputStream.write(buf, 0, len);
|
||||
sum += len;
|
||||
int progress = (int) (sum * 1.0f / total * 100);
|
||||
// 下载中
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
tvProgress.setText(progress + "%");
|
||||
progressBar.setProgress(progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
outputStream.flush();
|
||||
//启动安装app
|
||||
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
|
||||
handler.post(() -> progressDialog.dismiss());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
new Handler().post(() -> {
|
||||
listener.updateFailure(-1, e.getMessage());
|
||||
progressDialog.dismiss();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新监听
|
||||
*/
|
||||
public interface OnUpdateListener {
|
||||
void updateFailure(int code, String error);
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * 启动app安装
|
||||
// *
|
||||
// * @param context
|
||||
// * @param filePath
|
||||
// */
|
||||
// private static void startInstall(Context context, File filePath) {
|
||||
//
|
||||
// Intent install = new Intent(Intent.ACTION_VIEW);
|
||||
// install.setDataAndType(Uri.parse("file://" + filePath.getAbsolutePath()),
|
||||
// "application/vnd.android.package-archive");
|
||||
// install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
// context.startActivity(install);
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 安装apk
|
||||
*
|
||||
* @param context
|
||||
* @param file
|
||||
*/
|
||||
public void installApk(Context context, File file, String authority) {
|
||||
Intent intent = getInstallIntent(context, file, authority);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安装Intent
|
||||
*
|
||||
* @param context
|
||||
* @param file
|
||||
* @param authority
|
||||
* @return
|
||||
*/
|
||||
public Intent getInstallIntent(Context context, File file, String authority) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
Uri uriData;
|
||||
String type = "application/vnd.android.package-archive";
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
uriData = FileProvider.getUriForFile(context, authority, file);
|
||||
} else {
|
||||
uriData = Uri.fromFile(file);
|
||||
}
|
||||
intent.setDataAndType(uriData, type);
|
||||
return intent;
|
||||
}
|
||||
|
||||
public TextView tvProgress;
|
||||
public ProgressBar progressBar;
|
||||
public Dialog progressDialog;
|
||||
|
||||
private void showDialod(Activity context) {
|
||||
if (progressDialog == null) {
|
||||
progressDialog = new Dialog(context, R.style.dialog);
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
|
||||
tvProgress = view.findViewById(R.id.tvProgress);
|
||||
progressBar = view.findViewById(R.id.progressBar);
|
||||
progressDialog.setContentView(view);
|
||||
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = wm.getDefaultDisplay();
|
||||
int width = display.getWidth();
|
||||
Window window = progressDialog.getWindow();// 这部分是设置dialog宽高的,宽高是我从sharedpreferences里面获取到的。之前程序启动的时候有获取
|
||||
window.getDecorView().setPadding(0, 0, 0, 0);
|
||||
WindowManager.LayoutParams lp = window.getAttributes();
|
||||
lp.width = width / 4 * 3;
|
||||
window.setAttributes(lp);
|
||||
}
|
||||
progressDialog.setCancelable(false);
|
||||
progressDialog.show();
|
||||
}
|
||||
|
||||
}
|
@ -2,37 +2,26 @@ package com.yunbao.common.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.king.app.dialog.AppDialog;
|
||||
import com.king.app.updater.AppUpdater;
|
||||
import com.king.app.updater.UpdateConfig;
|
||||
import com.king.app.updater.callback.UpdateCallback;
|
||||
import com.king.app.updater.http.OkHttpManager;
|
||||
import com.yunbao.common.CommonAppConfig;
|
||||
import com.yunbao.common.CommonAppContext;
|
||||
import com.yunbao.common.R;
|
||||
import com.yunbao.common.bean.ConfigBean;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class VersionUtil {
|
||||
|
||||
private static String sVersion;
|
||||
|
||||
public TextView tvProgress;
|
||||
public ProgressBar progressBar;
|
||||
|
||||
/**
|
||||
* 是否是最新版本
|
||||
*/
|
||||
@ -49,176 +38,122 @@ public class VersionUtil {
|
||||
else return false;
|
||||
// return curVersion.equal(version);
|
||||
}
|
||||
public void updateProgress(long progress, long total){
|
||||
if(tvProgress == null || progressBar == null){
|
||||
return;
|
||||
}
|
||||
if(progress > 0){
|
||||
int currProgress = (int)(progress * 1.0f / total * 100.0f);
|
||||
tvProgress.setText(currProgress + "%");
|
||||
progressBar.setProgress(currProgress);
|
||||
}else{
|
||||
tvProgress.setText("稍等");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void upd( Activity context, ConfigBean configBean, String downloadUrl){
|
||||
UpdateConfig config = new UpdateConfig();
|
||||
config.setUrl(downloadUrl);
|
||||
config.addHeader("token","xxxxxx");
|
||||
AppUpdater mAppUpdater = new AppUpdater(context,config)
|
||||
.setHttpManager(OkHttpManager.getInstance())
|
||||
.setUpdateCallback(new UpdateCallback() {
|
||||
|
||||
@Override
|
||||
public void onDownloading(boolean isDownloading) {
|
||||
if(isDownloading){
|
||||
ToastUtil.show("已经在下载中,请勿重复下载。");
|
||||
}else{
|
||||
// showToast("开始下载…");
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.dialog_progress,null);
|
||||
tvProgress = view.findViewById(R.id.tvProgress);
|
||||
progressBar = view.findViewById(R.id.progressBar);
|
||||
AppDialog.INSTANCE.showDialog(context,view,false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(String url) {
|
||||
updateProgress(0,100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(long progress, long total, boolean isChange) {
|
||||
if(isChange){
|
||||
updateProgress(progress,total);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish(File file) {
|
||||
AppDialog.INSTANCE.dismissDialog();
|
||||
ToastUtil.show("下载完成");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
AppDialog.INSTANCE.dismissDialog();
|
||||
ToastUtil.show("下载失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
AppDialog.INSTANCE.dismissDialog();
|
||||
ToastUtil.show("取消下载");
|
||||
}
|
||||
});
|
||||
mAppUpdater.start();
|
||||
}
|
||||
//是否是谷歌版本
|
||||
public void showDialog( Activity context, ConfigBean configBean, String downloadUrl) {
|
||||
//不是谷歌
|
||||
if (CommonAppConfig.IS_GOOGLE_PLAY == false) {
|
||||
//不强更
|
||||
if(configBean.getForceUpdate()!=1){
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
public void showDialog(Activity context, ConfigBean configBean, String downloadUrl) {
|
||||
//不是谷歌
|
||||
if (CommonAppConfig.IS_GOOGLE_PLAY == false) {
|
||||
//不强更
|
||||
if (configBean.getForceUpdate() != 1) {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
context.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
upd(context,configBean,downloadUrl);
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
context.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// upd(context,configBean,downloadUrl);
|
||||
new APKDownloadUtil().downloadAPK(context, configBean.getDownloadApkUrl(), new APKDownloadUtil.OnUpdateListener() {
|
||||
@Override
|
||||
public void updateFailure(int code, String error) {
|
||||
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
}else{
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
context.finish();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
upd(context,configBean,downloadUrl);
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
}
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
} else {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
context.finish();
|
||||
}
|
||||
|
||||
//谷歌版本
|
||||
} else if (CommonAppConfig.IS_GOOGLE_PLAY == true) {
|
||||
//谷歌强制更新
|
||||
if(configBean.getGoogle_isup().equals("1")) {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
context.finish();
|
||||
}
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
// upd(context,configBean,downloadUrl);
|
||||
new APKDownloadUtil().downloadAPK(context, configBean.getDownloadApkUrl(), new APKDownloadUtil.OnUpdateListener() {
|
||||
@Override
|
||||
public void updateFailure(int code, String error) {
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.pdlive.shayu"));
|
||||
context.startActivity(i);
|
||||
context.finish();
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
//谷歌不强制
|
||||
}else {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.pdlive.shayu"));
|
||||
context.startActivity(i);
|
||||
context.finish();
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
}
|
||||
//谷歌版本
|
||||
} else if (CommonAppConfig.IS_GOOGLE_PLAY == true) {
|
||||
//谷歌强制更新
|
||||
if (configBean.getGoogle_isup().equals("1")) {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
context.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.pdlive.shayu"));
|
||||
context.startActivity(i);
|
||||
context.finish();
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
//谷歌不强制
|
||||
} else {
|
||||
DialogUitl.Builder builder = new DialogUitl.Builder(context);
|
||||
builder.setTitle(WordUtil.getString(R.string.version_update))
|
||||
.setContent(configBean.getUpdateDes())
|
||||
.setConfrimString(WordUtil.getString(R.string.version_immediate_use))
|
||||
.setCancelString(WordUtil.getString(R.string.version_not_update))
|
||||
.setCancelable(false)
|
||||
.setClickCallback(new DialogUitl.SimpleCallback2() {
|
||||
public void onCancelClick() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmClick(Dialog dialog, String content) {
|
||||
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.pdlive.shayu"));
|
||||
context.startActivity(i);
|
||||
context.finish();
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
12
common/src/main/res/drawable/app_dialog_bg.xml
Normal file
12
common/src/main/res/drawable/app_dialog_bg.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
|
||||
<solid android:color="@android:color/white" />
|
||||
|
||||
<corners
|
||||
android:bottomLeftRadius="10dp"
|
||||
android:bottomRightRadius="10dp"
|
||||
android:topLeftRadius="10dp"
|
||||
android:topRightRadius="10dp" />
|
||||
|
||||
</shape>
|
@ -19,7 +19,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_toRightOf="@+id/ivIcon"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/app_dialog_title_color"
|
||||
android:textColor="#333333"
|
||||
android:text="更新中"/>
|
||||
|
||||
<TextView
|
||||
@ -30,7 +30,7 @@
|
||||
android:layout_marginTop="10dp"
|
||||
android:lineSpacingMultiplier="1.2"
|
||||
android:text="正在下載中......"
|
||||
android:textColor="@color/app_dialog_content_color"
|
||||
android:textColor="#333333"
|
||||
android:textSize="14sp" />
|
||||
|
||||
<ProgressBar
|
||||
|
@ -4,9 +4,8 @@ ext {
|
||||
buildToolsVersion: "28.0.3",
|
||||
minSdkVersion : 21,
|
||||
targetSdkVersion : 31,
|
||||
versionCode : 400,
|
||||
|
||||
versionName : "6.4.5"
|
||||
versionCode : 401,
|
||||
versionName : "6.4.6"
|
||||
]
|
||||
manifestPlaceholders = [
|
||||
//正式
|
||||
|
Loading…
Reference in New Issue
Block a user