自动更新实现原理:两次访问服务器,检测、下载、安装三步走。

先访问服务器上面的版本信息,如果等于当前App版本号做相应处理例如toast一下这就是最新版本等,如果服务器所返回的版本号大于当前App版本号那么此时手机所安装的App不是最新版。弹出对话框提示用户是否升级,用户选择升级再访问安装包地址下载安装包,弹出进度框显示下载进度。下载完成后跳转安装页面安装APP。

准备工作:联网框架,文件访问权限,URI 临时访问权限

1.联网框架(用自己的就好了)

2.清单文件添加权限

<uses-permission android:name="android.permission.INTERNET" />
<!-- 写入权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

3.URI临时访问权限(使用 FileProvider类)

    3.1 清单文件 application下添加provider

//注意: 
//grantUriPermissions:必须是true,表示授予 URI 临时访问权限 
//exported:必须是false 
//resource:中的@xml/file_paths是我们接下来要添加的文件,名字自己定义
  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
   </provider>

   3.2 在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件(名字自己定义的那个)

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="beta_external_path" path="Download/"/>
    <!--.代表所有路径-->
    <external-path name="root_path" path="." />
</paths>

具体代码下面这样:

public class UpdateAppManager {

    //下载存放文件夹路径
    private static final String FILE_PATH = Environment.getExternalStorageDirectory() +"/" + "AutoUpdate" +"/";
    // 下载应用存放路径
    private static final String FILE_NAME = FILE_PATH + "AutoUpdate.apk";
    // 准备安装新版本应用标记
    private static final int INSTALL_TOKEN = 1;
    //Log日志打印标签
    private static final String TAG = "Update_log";

    private Context context;
    //获取新版APK的默认地址
    private String apk_path = "";
    // 下载应用的进度条
    private ProgressDialog progressDialog;

    //新版本号和描述语言
    private int update_versionCode;
    private String update_describe;
    public UpdateAppManager(Context context) {
        this.context = context;
    }

    /**
     * 获取当前版本号
     * @return
     */
    private int getCurrentVersion() {
        try {
            PackageManager manager=context.getPackageManager();
            PackageInfo info=manager.getPackageInfo(context.getPackageName(),1);
            Log.e(TAG, "当前版本名和版本号:" + info.versionName + "--" + info.versionCode);

            return info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            Log.e(TAG, "获取当前版本号出错:"+e.toString());
            return 1;
        }
    }
  /**
     * 从服务器获得更新信息
     */
    public void getUpdateMsg() {
        ArrayList<String> param = new ArrayList<String>();
        ArrayList<String> value = new ArrayList<String>();
        String methodName = "getVersion";
        RequestBody body = RequestBody.create(MediaType.parse("text/xml; charset=utf-8"), RetrofitUtils.getInstance().getRequestData(methodName, param, value));
        RetrofitUtils
                .getInstance()
                .getRetrofit()
                .getVersion(body)
                .subscribeOn(Schedulers.io())  // 网络请求切换在io线程中调用
                .unsubscribeOn(Schedulers.io())// 取消网络请求放在io线程
                .observeOn(AndroidSchedulers.mainThread())// 观察后放在主线程调用
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.d(TAG, "异步消息处理反馈--" + s);
                        try {

                          //  JSONObject object = JsonHelper.getJsonObject(s);
//模拟数据库请求过来的版本信息
JSONObject object=new JSONObject("[{result:true,version:'2',describe:'更新优化',apkUrl:'http://211.149.223.23:8093/JLBhandset/JLBHandset.apk'}]");
                            String result = object.optString("result");
                            if (result.equals("true")) {
                                update_versionCode = object.getInt("version");
                                update_describe = object.getString("describe");
                                apk_path=object.getString("apkUrl");
                            }
                            Log.d(TAG, "新版本号--" + update_versionCode);
                            Log.d(TAG, "新版本描述--\n" + update_describe);
                            if (update_versionCode > getCurrentVersion()) {

                                Log.d(TAG, "提示更新!");
                                showNoticeDialog();
                            } else {
                                Log.d(TAG, "已是最新版本!");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(context,""+e,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    /**
     * 显示提示更新对话框
     */
    private void showNoticeDialog() {
        new AlertDialog.Builder(context)
                .setTitle("检测到新版本!")
                .setMessage(update_describe)
                .setPositiveButton("下载", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showDownloadDialog();
                    }
                }).setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create().show();
    }
    /**
     * 显示下载进度对话框
     */
    public void showDownloadDialog() {

        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("正在下载...");
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        new downloadAsyncTask().execute();
    }
    /**
     * 下载新版本应用
     */
    private class downloadAsyncTask extends AsyncTask<Void, Integer, Integer> {

        @Override
        protected void onPreExecute() {
            Log.e(TAG, "执行至--onPreExecute");
            progressDialog.show();
        }

        @Override
        protected Integer doInBackground(Void... params) {

            Log.e(TAG, "执行至--doInBackground");

            URL url;
            HttpURLConnection connection = null;
            InputStream in = null;
            FileOutputStream out = null;
            try {
                url = new URL(apk_path);
                connection = (HttpURLConnection) url.openConnection();

                in = connection.getInputStream();
                long fileLength = connection.getContentLength();
                File file_path = new File(FILE_PATH);
                if (!file_path.exists()) {
                    file_path.mkdir();
                }

                out = new FileOutputStream(new File(FILE_NAME));//为指定的文件路径创建文件输出流
                byte[] buffer = new byte[1024 * 1024];
                int len = 0;
                long readLength = 0;

                Log.e(TAG, "执行至--readLength = 0");

                while ((len = in.read(buffer)) != -1) {

                    out.write(buffer, 0, len);//从buffer的第0位开始读取len长度的字节到输出流
                    readLength += len;

                    int curProgress = (int) (((float) readLength / fileLength) * 100);

                    Log.e(TAG, "当前下载进度:" + curProgress);

                    publishProgress(curProgress);

                    if (readLength >= fileLength) {

                        Log.e(TAG, "执行至--readLength >= fileLength");
                        break;
                    }
                }

                out.flush();
                return INSTALL_TOKEN;

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (connection != null) {
                    connection.disconnect();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            Log.e(TAG, "异步更新进度接收到的值:" + values[0]);
            progressDialog.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Integer integer) {

            progressDialog.dismiss();//关闭进度条
            //安装应用
            installApp();
        }
    }

    /**
     * 安装新版本应用
     */
    private void installApp() {
        File appFile = new File(FILE_NAME);
        if (!appFile.exists()) {
            return;
        }
        // 跳转到新版本应用安装页面
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //如果SDK版本>=24
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "com.jinheng.zz.javahandset.fileprovider", appFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
}

最后,调用:

private void initUpdate() {
       UpdateAppManager manager=new UpdateAppManager(this);
       manager.getUpdateMsg();//检查更新
    }

完事,超级简单,缺点:没有断点续传,下载判断,每次都重新下载。后面有空再修改吧