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

来源:互联网 发布:知字的草书写法 编辑:程序博客网 时间:2024/05/29 11:12
方法一:点击后跳转到调用原生浏览器下载
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);    }}
1 0
原创粉丝点击