App强制更新时,点击进行下载的逻辑,分为调用原生浏览器下载和自定义显示进度条下载两种

方法一:点击后跳转到调用原生浏览器下载
Uri uri = Uri.parse(upDataUrl);//直接调用原生的浏览器进行下载的方法
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
getActivity().finish();
方法二:自定义的显示进度条下载(不依赖App本身的网络请求框架实现的一个下载)
public class AppUpdateUtil {
private Context context;
private String upDateUrl;
// 外存sdcard存放路径
private static final String FILE_PATH = Environment.getExternalStorageDirectory() + "/" + "AutoUpdate" + "/";
// 下载应用存放全路径
private static final String FILE_NAME = FILE_PATH + "AutoUpdate.apk";
// 准备安装新版本应用标记
private static final int INSTALL_TOKEN = 1;
// 下载应用的进度条
private ProgressDialog progressDialog;
//Log日志打印标签
private static final String TAG = "Update_log";
//构造方法
public AppUpdateUtil(Context context, String upDataUrl) {
    this.context = context;
    this.upDateUrl = upDataUrl;
}
/**
 * 显示下载进度对话框
 */
public void showDownloadDialog() {
    //context
    progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("正在下载...");
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    new downloadAsyncTask().execute();
}
/**
 * 下载新版本应用
 */
private class downloadAsyncTask extends AsyncTask<Void, Integer, Integer> {
    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }
    @Override
    protected Integer doInBackground(Void... params) {
        URL url;
        HttpURLConnection connection = null;
        InputStream in = null;
        FileOutputStream out = null;
        try {
            url = new URL(upDataUrl);//下载安装的url路径
            connection = (HttpURLConnection) url.openConnection();
            in = connection.getInputStream();
            long fileLength = connection.getContentLength();
            File file_path = new File(FILE_PATH);
            if (!file_path.exists()) {
                file_path.mkdir();
            }
            out = new FileOutputStream(new File(FILE_NAME));//为指定的文件路径创建文件输出流
            byte[] buffer = new byte[1024 * 1024];
            int len = 0;
            long readLength = 0;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);//从buffer的第0位开始读取len长度的字节到输出流
                readLength += len;
                int curProgress = (int) (((float) readLength / fileLength) * 100);
                publishProgress(curProgress);
                if (readLength >= fileLength) {
                    break;
                }
            }
            out.flush();
            return INSTALL_TOKEN;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }
    @Override
    protected void onPostExecute(Integer integer) {
        progressDialog.dismiss();//关闭进度条
        //安装应用
        installApp();

    }
}
/**
 * 安装新版本应用
 */
private void installApp() {
    File appFile = new File(FILE_NAME);
    if (!appFile.exists()) {
        return;
    }
    // 跳转到新版本应用安装页面
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://" + appFile.toString()), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这一句非常重要,不然安装完成后不能让用户点击打开进行重启(就是会直接关闭app,不会出现提示用户点击完成、打开的界面)
    context.startActivity(intent);
    }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值