安卓app自动更新功能完美实现

示例:
安卓app自动更新功能完美实现_第1张图片

零、准备工作

0.1第三方库

implementation ‘io.reactivex.rxjava2:rxjava:2.2.2’
implementation ‘io.reactivex.rxjava2:rxandroid:2.1.0’
implementation ‘io.reactivex.rxjava2:rxkotlin:2.3.0’
implementation ‘com.squareup.okhttp3:okhttp:3.11.0’
implementation ‘com.squareup.okio:okio:2.0.0’

0.2权限





0.3格式

“Code”: 0,
“Msg”: “”,
“UpdateStatus”: 1,
“VersionCode”: 3,
“VersionName”: “1.0.2”,
“ModifyContent”: “1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。”,
“DownloadUrl”: “https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk”,
“ApkSize”: 2048
“ApkMd5”: “…” //md5值没有的话,就无法保证apk是否完整,每次都会重新下载。

一、检测是否是最新版本,不是则更新

private Disposable downDisposable;
private ProgressBar progressBar;
private TextView textView4;
private Button upgrade;
private long downloadLength=0;
private long contentLength=0;
private String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE};

//判断版本是否最新,如果不是最新版本则更新

private void test(){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("url")
                        .build();

                client.newCall(request).enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        emitter.onError(e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result="";
                        if (response.body()!=null) {
                            result=response.body().string();
                        }else {
                            //返回数据错误
                            return;
                        }
                        emitter.onNext(result);
                    }
                });
//                emitter.onNext("123");
            }
        }).subscribeOn(Schedulers.io())// 将被观察者切换到子线程
                .observeOn(AndroidSchedulers.mainThread())// 将观察者切换到主线程
                .subscribe(new Observer() {
                    private Disposable mDisposable;
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }
                    @Override
                    public void onNext(String result) {
                        if (result.isEmpty()){
                            return;
                        }
                        //2.判断版本是否最新,如果不是最新版本则更新
                        String downloadUrl="https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk";
                        String title="是否升级到4.1.1版本?";
                        String size="新版本大小:未知";
                        String msg="1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。";
                        int versionCode=20000;
                        try {
                            int version = getPackageManager().
                                    getPackageInfo(getPackageName(), 0).versionCode;
                            if (versionCode>version){
                                LayoutInflater inflater = LayoutInflater.from(TestActivity.this);
                                View view = inflater.inflate(R.layout.layout_dialog, null);
                                AlertDialog.Builder mDialog = new AlertDialog.Builder(TestActivity.this,R.style.Translucent_NoTitle);
                                mDialog.setView(view);
                                mDialog.setCancelable(true);
                                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                                    @Override
                                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                        return keyCode == KeyEvent.KEYCODE_BACK;
                                    }
                                });
                                upgrade= view.findViewById(R.id.button);
                                TextView textView1= view.findViewById(R.id.textView1);
                                TextView textView2= view.findViewById(R.id.textView2);
                                TextView textView3= view.findViewById(R.id.textView3);
                                textView4= view.findViewById(R.id.textView4);
                                ImageView iv_close= view.findViewById(R.id.iv_close);
                                progressBar= view.findViewById(R.id.progressBar);
                                progressBar.setMax(100);
                                textView1.setText(title);
                                textView2.setText(size);
                                textView3.setText(msg);
                                upgrade.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //动态询问是否授权
                                        int permission = ActivityCompat.checkSelfPermission(getApplication(),
                                            Manifest.permission.WRITE_EXTERNAL_STORAGE);
                                        if (permission != PackageManager.PERMISSION_GRANTED) {
                                            ActivityCompat.requestPermissions(TestActivity.this, PERMISSIONS_STORAGE,
                                                1);
                                        }else {
                                            upgrade.setVisibility(View.INVISIBLE);
                                            down(downloadUrl);
                                        }
                                    }
                                });
                                iv_close.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        finish();
                                    }
                                });
                                mDialog.show();
                            }else {

                            }
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                        }
                        mDisposable.dispose();
                    }
                    @Override
                    public void onError(Throwable e) {
                        test();
                    }
                    @Override
                    public void onComplete() {

                    }
                });
    }

//下载apk并更新进度条

private void down(String downloadUrl){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                downApk(downloadUrl,emitter);
            }
        }).subscribeOn(Schedulers.io())// 将被观察者切换到子线程
                .observeOn(AndroidSchedulers.mainThread())// 将观察者切换到主线程
                .subscribe(new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        downDisposable = d;
                    }
                    @Override
                    public void onNext(Integer result) {
                        //设置ProgressDialog 进度条进度
                        progressBar.setProgress(result);
                        textView4.setText(result+"%");
                    }
                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(getApplication(),"网络异常!请重新下载!",Toast.LENGTH_SHORT).show();
                        upgrade.setEnabled(true);
                    }
                    @Override
                    public void onComplete() {
                        Toast.makeText(getApplication(),"服务器异常!请重新下载!",Toast.LENGTH_SHORT).show();
                        upgrade.setEnabled(true);
                    }
                });
    }

二、下载apk

//下载apk

private void downApk(String downloadUrl,ObservableEmitter emitter){
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(downloadUrl)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //下载失败
            breakpoint(downloadUrl,emitter);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.body() == null) {
                //下载失败
                breakpoint(downloadUrl,emitter);
                return;
            }
            InputStream is = null;
            FileOutputStream fos = null;
            byte[] buff = new byte[2048];
            int len;
            try {
                is = response.body().byteStream();
                File file = createFile();
                fos = new FileOutputStream(file);
                long total = response.body().contentLength();
                contentLength=total;
                long sum = 0;
                while ((len = is.read(buff)) != -1) {
                    fos.write(buff,0,len);
                    sum+=len;
                    int progress = (int) (sum * 1.0f / total * 100);
                    //下载中,更新下载进度
                    emitter.onNext(progress);
                    downloadLength=sum;
                }
                fos.flush();
                //4.下载完成,安装apk
                installApk(TestActivity.this,file);
            } catch (Exception e) {
                e.printStackTrace();
                breakpoint(downloadUrl,emitter);
            } finally {
                try {
                    if (is != null)
                        is.close();
                    if (fos != null)
                        fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

}

//断点续传

private void breakpoint(String downloadUrl,ObservableEmitter emitter){
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(downloadUrl)
            .addHeader("RANGE", "bytes=" + downloadLength + "-" + contentLength)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //下载失败
            breakpoint(downloadUrl,emitter);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.body() == null) {
                //下载失败
                breakpoint(downloadUrl,emitter);
                return;
            }
            InputStream is = null;
            RandomAccessFile randomFile = null;
            byte[] buff = new byte[2048];
            int len;
            try {
                is = response.body().byteStream();
                String root = Environment.getExternalStorageDirectory().getPath();
                File file = new File(root,"updateDemo.apk");
                randomFile = new RandomAccessFile(file, "rwd");
                randomFile.seek(downloadLength);
                long total = contentLength;
                long sum = downloadLength;
                while ((len = is.read(buff)) != -1) {
                    randomFile.write(buff,0,len);
                    sum+=len;
                    int progress = (int) (sum * 1.0f / total * 100);
                    //下载中,更新下载进度
                    emitter.onNext(progress);
                    downloadLength=sum;
                }
                //4.下载完成,安装apk
                installApk(TestActivity.this,file);
            } catch (Exception e) {
                e.printStackTrace();
                breakpoint(downloadUrl,emitter);
            } finally {
                try {
                    if (is != null)
                        is.close();
                    if (randomFile != null)
                        randomFile.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

/**

  • 路径为根目录
  • 创建文件名称为 updateDemo.apk
    */
private File createFile() {
    String root = Environment.getExternalStorageDirectory().getPath();
    File file = new File(root,"updateDemo.apk");
    if (file.exists())
        file.delete();
    try {
        file.createNewFile();
        return file;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null ;
}

三、安装apk

3.1项目的src/res新建个xml文件夹再自定义一个file_paths文件



    

3.2在清单文件中配置


    

3.3安装apk

//安装apk,包含7.0

public void installApk(Context context, File file) {
    if (context == null) {
        return;
    }
    String authority = getApplicationContext().getPackageName() + ".fileProvider";
    Uri apkUri = FileProvider.getUriForFile(context, authority, file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //判读版本是否在7.0以上
    if (Build.VERSION.SDK_INT >= 24) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    } else {
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    }

    context.startActivity(intent);
    //弹出安装窗口把原程序关闭。
    //避免安装完毕点击打开时没反应
    Process.killProcess(android.os.Process.myPid());
}

四、取消订阅

@Override
protected void onDestroy() {
    super.onDestroy();
    downDisposable.dispose();//取消订阅
}

五、自定义Dialog

5.1UI

见一、检测是否是最新版本,不是则更新

5.2布局


xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>












    

        
    


5.3其他文件

5.3.1textview_round_red.xml




    
    
    
    
    
    

5.3.2lib_update_app_info_bg.xml



    
    

5.3.3styles文件


5.4图片

安卓app自动更新功能完美实现_第2张图片

在这里插入图片描述

你可能感兴趣的:(自动更新)