参考博客: https://blog.csdn.net/qq_29405933/article/details/82862679
1.从服务器获取版本信息:
(可参考我上一篇博客:https://blog.csdn.net/qq_30297763/article/details/87875072)
private void upapk(){
OkGo.<String>get(UrlUtil.UPAPK)
.tag(this)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
UpdateBean updateBean = FastJsonUtils.getObject(response.body(),UpdateBean.class);
versionCode = updateBean.getVersionCode();
versionName = updateBean.getVersionName();
versionUrl = updateBean.getApkurl();
versionMessage = updateBean.getMessage();
//判断是否更新
if (updateJudge()){
dialogUp();
}else {
//软件为最低版本
}
}
});
}
2.通过对比版本号判断是否更新:
(对比版本名称可参考我这篇博客:https://blog.csdn.net/qq_30297763/article/details/77884045)
private boolean updateJudge(){
PackageManager packageManager = getPackageManager();
PackageInfo packInfo = null;
try {
packInfo = packageManager.getPackageInfo(getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (Integer.parseInt(versionCode) > packInfo.versionCode ){
return true;
}else {
return false;
}
}
3.如果版本有更新弹窗提示:
(因需要,使用户必须强制更新)
/**
* 3.弹窗更新提示
*/
private void dialogUp(){
AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(this);
// alertDialogBuilder.setIcon(R.drawable.)
alertDialogBuilder.setTitle("版本更新");
alertDialogBuilder.setMessage(versionMessage);
alertDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//更新应用提醒
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("软件更新中,请稍等~");
progressDialog.setCancelable(true);
progressDialog.show();
getFile(); //下载apk
}
});
alertDialogBuilder.setCancelable(false);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();//将dialog显示出来
}
4.下载APK:
private void getFile(){
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download";
final String fileName = "dzjcy.apk";
final File file = new File(filePath + "/" + fileName);
if (file.exists()){
file.delete();
}
OkGo.<File>get(versionUrl)
.tag(this)
.execute(new FileCallback(filePath, fileName) {
@Override
public void onStart(Request<File, ? extends Request> request) {
super.onStart(request);
Log.e("MainActivity", "开始下载" );
}
@Override
public void onSuccess(Response<File> response) {
Log.e("MainActivity", "file--" + file);
openFile(file); //安装apk
}
@Override
public void onError(Response<File> response) {
super.onError(response);
Log.e("MainActivity", "onError: "+response.message());
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
Log.e("MainActivity", "progress" + progress.fraction * 100);
}
});
}
5.安装apk:
private void openFile(File file) {
//Android 7.0及以上
if (Build.VERSION.SDK_INT >= 24) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
boolean hasInstallPermission = this.getPackageManager().canRequestPackageInstalls();
if (!hasInstallPermission) {
//请求安装未知应用来源的权限
ActivityCompat.requestPermissions((Activity) this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 6666);
}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri apkUri = FileProvider.getUriForFile(this, "com.dzjcy.provider.fileprovider", file);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
this.startActivity(intent);
} else {
Log.e("MainActivity", "小于24" );
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
this.startActivity(intent);
}
}
6.在AndroidManifest.xml内 的 application 创建provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.dzjcy.provider.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
- 在xml文件夹下创建 filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="myFile" path="."/>
</paths>
注:
7.0以上需要获取安装权限:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />