在很多APP中版本更新是最基本的一个功能了,那么从检查版本更新到下载自动安装这一系列过程改如何实现呢,当然有很多方式,这里我总结了一种其中很基本的方式。 这是效果图:
检查更新这个就不多说啦,这里我用的是通过OkHttpUtils网络请求框架来检测新版本。当然OkHttpUtils也有自己的下载功能,但是好像有很多限制,其中文件过大就会下载失败内存溢出等问题。 - public void checkUpdate(Activity activity, final boolean isShowTip) {
- OkHttpUtils.get().url(Url.UPDATE_VERSION).build().execute(new StringCallback() {
- @Override
- public void onError(Call call, Exception e, int id) {
-
- }
-
- @Override
- public void onResponse(String response, int id) {
- final JsonData jsonData = JsonData.create(response);
- int versionCode = jsonData.optInt("version");
- int currentVersionCode = SystemUtils.getAppVersionCode(context);
- if (versionCode > currentVersionCode) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle("更新");
- builder.setMessage("检测到有更新,是否立刻更新?");
- builder.setNegativeButton("稍后更新", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.setPositiveButton("立刻更新", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (!StringUtils.isWifi(context)) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle("提示");
- builder.setMessage("您当前正在使用移动网络,继续下载将消耗流量");
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- downLoadApk(jsonData.optString("download"));
- }
- });
- builder.create().show();
- } else {
- downLoadApk(jsonData.optString("download"));
- }
- }
- });
- builder.create().show();
- } else {
- if (isShowTip) {
- ToastUtils.show(context, "当前已是最新版本");
- }
- }
- }
-
- });
- }
-
- /*
- * 从服务器中下载APK
- */
- protected void downLoadApk(final String url) {
- final ProgressDialog pd; //进度条对话框
- pd = new ProgressDialog(this);
- pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- pd.setMessage("正在下载更新");
- pd.show();
- new Thread() {
- @Override
- public void run() {
- try {
- File file = DownLoadManager.getFileFromServer(url, pd);
- sleep(3000);
- installApk(file);
- pd.dismiss(); //结束掉进度条对话框
- } catch (Exception e) {
- Toast.makeText(context, "下载失败!",
- Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- }
- }.start();
- }
-
- //安装apk
- protected void installApk(File file) {
- Intent intent = new Intent();
- //执行动作
- intent.setAction(Intent.ACTION_VIEW);
- //执行的数据类型
- intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
- startActivity(intent);
- }
复制代码这里是下载数据的代码DownLoadManager.java - import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- import android.app.ProgressDialog;
- import android.os.Environment;
-
- public class DownLoadManager {
-
- public static File getFileFromServer(String path, ProgressDialog pd) throws Exception {
- //如果相等的话表示当前的sdcard挂载在手机上并且是可用的
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- //获取到文件的大小
- pd.setMax(conn.getContentLength());
- InputStream is = conn.getInputStream();
- File file = new File(Environment.getExternalStorageDirectory(), "某某某.apk");
- FileOutputStream fos = new FileOutputStream(file);
- BufferedInputStream bis = new BufferedInputStream(is);
- byte[] buffer = new byte[1024];
- int len;
- int total = 0;
- while ((len = bis.read(buffer)) != -1) {
- fos.write(buffer, 0, len);
- total += len;
- //获取当前下载量
- pd.setProgress(total);
- }
- fos.close();
- bis.close();
- is.close();
- return file;
- } else {
- return null;
- }
- }
- }
复制代码
|