apk下载安装

1.简单的示例

//点击下载apk并安装
    public void button_apk(View view) {
        //主线程显示提示视图
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        //file对象存放在sd卡中(公有的)
        apkFile = new File(getExternalFilesDir(null), "update.apk");
        //启动子线程 下载apk 并显示进度
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String path = "http://192.168.1.5:8080/20190313/app-debug.apk";
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(10000);
                    connection.connect();
                    //得到响应
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        progressDialog.setMax(connection.getContentLength());//文件大小
                        InputStream is = connection.getInputStream();
                        FileOutputStream fos = new FileOutputStream(apkFile);
                        //边读边写
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while ((len = is.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                            //显示下载进度
                            progressDialog.incrementProgressBy(len); //注意设置他的最大值(最大值等于文件大小//

                            SystemClock.sleep(50);
                        }
                        is.close();
                        fos.close();
                    }
                    //下载完成 关闭
                    connection.disconnect();
                    //切换到主线程 启动安装
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressDialog.dismiss(); //取消精度条
                            //安装apk
                            installApk();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //安装apk
    private void installApk() {
        Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        startActivity(intent);
    }

动态获取权限的问题 更美的进度条 等等 这只是个最基础的示例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值