android菜鸟低调修行纪念册

apk 内部版本更新 完美兼容Android 7.0

2018-06-11  本文已影响260人  大写的口可口可

话不多说先上图


图片.png

第一步:下载apk 文件。

private class DownloadApk implements Runnable {
        private ProgressDialog dialog;
        InputStream is;
        FileOutputStream fos;

        public DownloadApk(ProgressDialog dialog) {
            this.dialog = dialog;
        }

        @Override
        public void run() {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .addHeader("Accept-Encoding", "identity").get().url(mDownLoadURL).build();
            try {
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    long contentLength = response.body().contentLength();
                    dialog.setMax((int) contentLength);
                    File apkFile = new File(Environment.getExternalStorageDirectory()
                            ,  "um.apk");
                    fos = new FileOutputStream(apkFile);
                    is = response.body().byteStream();
                    byte[] bys = new byte[1024];
                    int progress = 0;
                    int len = -1;
                    while ((len = is.read(bys)) != -1) {
                        try {
                            Thread.sleep(1);
                            fos.write(bys, 0, len);
                            fos.flush();
                            progress += len;
                            int finalProgress = progress;
                            dialog.setProgress(finalProgress);
                        } catch (InterruptedException e) {
                            Message msg = Message.obtain();
                            msg.what = SHOW_ERROR;
                            msg.obj = "ERROR:10002";
                            handler.sendMessage(msg);
                            return;
                        }
                    }
                    //下载完成,提示用户安装
                    install(apkFile);
                }
            } catch (IOException e) {
                Message msg = Message.obtain();
                msg.what = SHOW_ERROR;
                msg.obj = "ERROR:10003";
                handler.sendMessage(msg);
                return;
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    is = null;
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fos = null;
                }
            }
            dialog.dismiss();
        }
    }

注意:默认从服务端下载的包为Gzip格式。这里需要在请求头添加这句话来设置默认编码格式。

addHeader("Accept-Encoding", "identity")

第二步:配置provider路径。
配置1.在AndroidManifest.xml 中添加provider

<application>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="你的包名.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

</application>

配置2:添加provider 资源文件在xml 中


图片.png
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="."
        path="." />
</paths>

第三部:安装apk

/** file 为apk下载文件
*/
private void install(File file) {
        File apkFile = new File(file.getPath());
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    MainActivity.this
                    , "你的包名.fileprovider"
                    , apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        startActivity(intent);
    }
上一篇下一篇

猜你喜欢

热点阅读