android根据url下载apk文件并更新

配置guild.gradle

需要导入OkGo网络框架

 implementation 'com.lzy.net:okgo:3.0.4'
 implementation 'com.lzy.net:okserver:2.0.5'

需要授予app允许安装外部apk的权限

 

需要自定义一个Provider

//空类Provider
public class MyFileProvider extends FileProvider
{

}

并在res文件夹中创建一个xml类型的xml文件file_paths.xml



    
    

之后需要在Manifest文件中配置provider


            
        

之后可以进行网络请求下载

private void updateApk(String downUrl)
    {
        final FileUtils fileUtils = FileUtils.getInstance();
        numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(2);
        up_layout.setVisibility(View.VISIBLE);
        OkGo.get(downUrl)//
                .tag(this)//
                .execute(new FileCallback()
                {

                    @Override
                    public void onStart(com.lzy.okgo.request.base.Request request)
                    {
//                        btnFileDownload.setText("正在下载中");
                    }

                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response response)
                    {
                        up_layout.setVisibility(View.GONE);
                        fileUtils.installApk(context, response.body().getName());
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response response)
                    {
                        fileUtils.downError(context);
                    }

                    @Override
                    public void downloadProgress(Progress progress)
                    {
                        String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize);//获取的文件大小
                        String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);//总文件大小
                        tvDownloadSize.setText(downloadLength + "/" + totalLength);//文件大小百分比
                        String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);//速度
                        tvNetSpeed.setText(String.format("%s/s", speed));//下载速度
                        tvProgress.setText(numberFormat.format(progress.fraction));//下载进度
                        pbProgress.setMax(10000);
                        pbProgress.setProgress((int) (progress.fraction * 10000));//下载条
                    }
                });
    }

在下载完成后需要调用安装


    public void installApk(Context context, String name)
    {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String type = "application/vnd.android.package-archive";
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            uri = FileProvider.getUriForFile(context, "com.example.yuwenhao.socketdemo.utils.MyFileProvider", getApkFile(name));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else
        {
            uri = Uri.fromFile(getApkFile(name));
        }
        intent.setDataAndType(uri, type);
        context.startActivity(intent);
    }

    public File getApkFile(String apkName)
    {
        String apkDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "download" + File.separator;
        File newApkFile = new File(apkDir, apkName);       //File()的第一个参数为文件的父路径,第二个参数是文件名
        return newApkFile;
    }

 

你可能感兴趣的:(网络请求)