package com.example.testupgrade;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private final String TAG = “Jason”;
private File file;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
if(versionCheck()) {
showTip(this);
}
}
//在主界面显示app版本号,非必要
private void initView() {
textView = findViewById(R.id.tv_version);
textView.setText(“version:” + getVersionName());
}
//新版本检测
private boolean versionCheck() {
String targetVersion = “xxx”; //todo 实际目标版本号应向平台查询
Log.d(TAG, “versionCheck version:” + getVersionName());
Log.d(TAG, “versionCheck targetVersion:” + targetVersion);
if(getVersionName().contentEquals(targetVersion)) {
return false;
} else {
return true;
}
}
//升级弹窗
private void showTip(Context context) {
AlertDialog dialog = new AlertDialog.Builder(context).setTitle(“升级提示”).setMessage(“检测到新版本,请升级”)
.setNeutralButton(“升级”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doDownload();
}
}).setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
dialog.setCanceledOnTouchOutside(false);//可选
dialog.setCancelable(false);//可选
}
//下载升级包
private void doDownload() {
String downloadUrl = “https://xxx”; //todo 实际下载地址应向平台查询
String parentPath = “”;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
parentPath = this.getExternalFilesDir(null).getPath();
} else {
parentPath = this.getFilesDir().getPath();
}
} catch (Exception e) {
Log.d(TAG, “doDownload e:” + e.getMessage());
}
Log.d(TAG, “doDownload parentPath:” + parentPath);
file = new File(parentPath, “myhouse.apk”);
final String filePath = file.getAbsolutePath();
//如果已有文件,删除
if (file.exists()) {
Log.d(TAG, “doDownload delete APK”);
file.delete();
}
try {
DownloadUtil.get().download(downloadUrl, filePath, new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess() {
//成功
Log.d(TAG, “doDownload download success”);
installApk();
}
@Override
public void onDownloading(int progress) {
//进度
//Log.d(TAG, “doDownload download:” + progress +”%”);
}
@Override
public void onDownloadFailed() {
//失败
Log.d(TAG, “doDownload download fail”);
}
});
} catch (Exception e) {
Log.d(TAG, “doDownload e2:” + e.getMessage());
}
}
//安装app
private void installApk() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data;
//7.0以上安卓系统安装app需要通过fileProvider(需要在AndroidManifest.xml声明)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
data = FileProvider.getUriForFile(this, “com.example.testupgrade.provider”, file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d(TAG,”installApk 7.0data:” + data);
} else {
data = Uri.fromFile(file);
Log.d(TAG,”installApk data:” + data);
}
intent.setDataAndType(data, “application/vnd.android.package-archive”);
this.startActivity(intent);
}
//获取软件版本号
private String getVersionName() {
String versionName = “”;
try {
versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
}
原创文章,作者:Carrie001128,如若转载,请注明出处:https://blog.ytso.com/273781.html