Android版本更新下载apk文件到sd卡并自动安装

版本更新,需要注意的是android7.0和8.0的安装问题

android8.0以上得需要安装未知来源应用的权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

android7.0

1.android7.0需要在清单文件注册

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.jetair.cuair.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

2.在res文件下新建xml文件夹,编写file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="downloadApk"
        path="cuair" />
</paths>

下载安装apk的代码:


    private void downLoadApk(final String downURL, final String appName){
        final ProgressDialog pd = new ProgressDialog(this);
        //必须一直下载完,不可取消
        pd.setCancelable(false);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载安装包");
//        pd.setTitle("版本更新");
        pd.show();

        Thread t = new Thread(){
            @Override
            public void run() {
                try {
                    File file = downloadFile(downURL, appName, pd);

                    sleep(1000);
                    // 结束掉进度条对话框
                    pd.dismiss();

                    if (file!=null){
                        installApk(file);
                    } else {
                        Toast.makeText(BrowserActivity.this, "更新包无法下载", Toast.LENGTH_LONG).show();
                    }

                } catch (Exception e) {
                    pd.dismiss();

                }
            }
        };

        t.start();
    }

    private File downloadFile(String path,String appName ,ProgressDialog pd) throws Exception {
        // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            // 获取到文件的大小
            pd.setMax(conn.getContentLength());
            InputStream is = conn.getInputStream();
            String fileName = Environment.getExternalStorageDirectory()+ "/cuair/" + appName+".apk";
            File file = new File(fileName);
            // 目录不存在创建目录
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            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;
        }
    }

    /**
     * 安装apk
     */
    private void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.d("installApk", "版本大于 N ,开始使用 fileProvider 进行安装");

            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(this, "com.jetair.cuair.fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            Log.d("installApk", "正常进行安装");

            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        startActivity(intent);
    }

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读