Android下载和安装简介

下载都没什么 只要是把权限搞好就行了

 @Override
//写在当前服务的启动方法中 
    public int onStartCommand(Intent intent, int flags, int startId) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.wandoujia.com/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpDatasService httpDatasService = retrofit.create(HttpDatasService.class);
        Observable observable = httpDatasService.downLoad();
        observable.subscribeOn(Schedulers.io())

                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i("tag", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        try {
                       //获取当前下载文件的大小
                            long contentLength = responseBody.contentLength();
                      //获取sdk的文件目录
                            File directory = Environment.getExternalStorageDirectory();
                      //创建文件
                            File file = new File(directory, "123.apk");
                      //获取输入流 创建输出流 下载文件到指定文件下
                            InputStream inputStream = responseBody.byteStream();
                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            byte[] bytes = new byte[1024*10];
                            int len;
                            while ((len=inputStream.read(bytes))!=-1) {
                                fileOutputStream.write(bytes,0,len);
                              //获取当前文件大小
                                long length = file.length();
                            //算出当前百分比
                                int current = (int) (length * 100 / contentLength);
                                Log.i("tag", "onNext: "+current);
                            EventBus传值
                                EventBus.getDefault().post(current);


                            }
                            EventBus.getDefault().post(file);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }

由于7.0以上需要配置fileprovider 所以安装变得稍微复杂了一些
fileprovider也是四大组件之一的contentprovider的特殊子类所以需要在清单列表中进行配置

  
        
        
        
        
            
        
            
            

        

其中android:resource="@xml/file_paths" 需要在res下创建一个xml文件夹





    
    
    

8.0以上需要注意的就是 加一个允许外部下载的权限


下面就是隐式启动的代码了


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getDownload(Integer current) {
        this.current = current;
        progressBar.setProgress(this.current);
        tvDown.setText("当前下载进度:" + current);
        if (current == 100) {
                btnOk.setText("下载完成");
                //当前版本小于24 N代表24 也就是小于7.0版本

        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void downloadApp(File file){
        if (current == 100) {
            //当前版本小于24 N代表24 也就是小于7.0版本
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri,"application/vnd.android.package-archive");
                startActivityForResult(intent,2);


            }else{
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Uri uriForFile = FileProvider.getUriForFile(this, "com.example" +
                        ".download2.provider", file);
                intent.setDataAndType(uriForFile,"application/vnd.android.package-archive");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加读取权限
                startActivityForResult(intent,2);


            }
        }
    }

你可能感兴趣的:(Android下载和安装简介)