private static final String TAG = "MainActivity";

private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 10000; // 写入SD卡权限

private ProgressDialog mDownloadDialog; // 加载进度

private String serverVersion; // 服务器版本号

private String description; // 新版本更新描述

private String apkUrl; // 新版本apk下载地址

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 检查版本更新

checkVersion();

}

/**

* 检查是否有新版本

*/

private void checkVersion() {

NetworkUtils.shared.get(APIs.UPDATE, new HashMap(), new NetworkUtils.StringCallback() {

@Override

public void onError(Call call, Exception e, int id) {

ProgressHUD.showInfo(mContext, "您的网络不给力");

}

@Override

public void onResponse(String response, int id) {

try {

JSONObject jsonObject = new JSONObject(response);

if (jsonObject.getString("err_msg").equals("success")) {

JSONObject versionInfo = jsonObject.getJSONObject("data");

serverVersion = versionInfo.getString("version");

description = versionInfo.getString("description");

apkUrl = versionInfo.getString("url");

// 更新版本

showUpdateDialog();

}

} catch (JSONException e) {

e.printStackTrace();

ProgressHUD.showInfo(mContext, "数据解析异常");

}

}

});

}

/**

* 弹出对话框更新app版本

*/

protected void showUpdateDialog() {

// 检查是否是新版本

String currentVersion = BaoKanApp.app.getVersionName();

if (currentVersion.equals(serverVersion)) {

return;

}

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("发现新版本:" + serverVersion);

builder.setIcon(R.mipmap.ic_launcher);

builder.setMessage(description);

builder.setCancelable(false);

builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// 判断是否有写入SD权限

if (ContextCompat.checkSelfPermission(mContext,

Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

// 申请权限

ActivityCompat.requestPermissions(mContext,

new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

} else {

// 有写入权限直接下载apk

downloadAPK();

}

}

});

builder.setNegativeButton("取消", null);

builder.show();

}

/**

* 运行时权限请求回调结果

*/

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode) {

case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

downloadAPK();

} else {

Toast.makeText(getApplicationContext(), "你没有文件写入权限", Toast.LENGTH_SHORT).show();

}

break;

}

}

/**

* 下载新版本

*/

protected void downloadAPK() {

// apk文件保存路径

String apkPath = null;

String apkName = "baokan" + serverVersion + ".apk";

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

apkPath = Environment.getExternalStorageDirectory().getAbsolutePath();

}

if (apkPath == null) {

ProgressHUD.showInfo(mContext, "您的手机没有SD卡");

return;

}

// 弹出下载进度会话框

showDownloadDialog();

// 下载文件

OkHttpUtils

.get()

.url(apkUrl)

.build()

.execute(new FileCallBack(apkPath, apkName) {

@Override

public void onResponse(File arg0, int arg1) {

mDownloadDialog.dismiss();

// 下载完成安装apk

installAPK(arg0.getAbsolutePath());

}

@Override

public void onError(Call arg0, Exception arg1, int arg2) {

mDownloadDialog.dismiss();

ProgressHUD.showInfo(mContext, "您的网络不给力哦");

}

@Override

public void inProgress(float progress, long total, int id) {

// 更新下载进度

mDownloadDialog.setProgress(Math.round(progress * 100));

}

});

}

/**

* 弹出下载对话框

*/

public void showDownloadDialog() {

mDownloadDialog = new ProgressDialog(mContext);

mDownloadDialog.setIcon(R.mipmap.ic_launcher);

mDownloadDialog.setTitle("版本更新");

mDownloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

mDownloadDialog.setMessage("正在玩命下载中......");

mDownloadDialog.getWindow().setGravity(Gravity.CENTER);

mDownloadDialog.setMax(100);

mDownloadDialog.show();

}

/**

* 安装下载的新版本apk

*

* @param apkPath apk存放路径

*/

private void installAPK(String apkPath) {

Intent intent = new Intent();

intent.setAction("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");

startActivity(intent);

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐