版本更新简要

首先 我们需要列出版本号和权限

android:versionCode="自定义"
android:versionName="自定义"

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>

建立一个类用上我们所需的数据
public class VersionBean {
    public int versionCode = 2;
    public String versionName;
    public String updateUrl = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk";

}

然后开始写代码

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btu = (Button) findViewById(R.id.btu);
        btu.setOnClickListener(this);

    }

    public void onClick(View view){
        try {
            // 模拟获取服务器版本信息
            final VersionBean versionBean = new VersionBean();
            //获取本地版本信息
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            int versionCode = packageInfo.versionCode;
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("检查更新");
            if (versionBean.versionCode > versionCode) {
                builder.setMessage("发现新的版本,是否更新?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        final ProgressDialog pd = new ProgressDialog(MainActivity.this);
                        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        pd.setMessage("正在下载最新版本");
                        pd.show();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                File file = updateVersion(pd, versionBean.updateUrl);
                                if (file != null) {
                                    insertApk(file);
                                    pd.dismiss();
                                }
                            }
                        }).start();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
            } else {
                builder.setMessage("已经是最新版本!");
            }
            builder.show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    private void insertApk(File file) {
        Intent intent = new Intent();
        //执行动作
        intent.setAction(Intent.ACTION_VIEW);
        //执行的数据类型
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");//编者按:此处Android应为android,否则造成安装不了
        startActivity(intent);
    }

    /**
     * 下载最新版本
     *
     * @param updateUrl
     */
    private File updateVersion(ProgressDialog pd,String updateUrl) {
        try {
            URL url = new URL(updateUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            if (200 == connection.getResponseCode()) {
                //获取文件大小
                pd.setMax(connection.getContentLength());
                File file = new File(Environment.getExternalStorageDirectory(), "update.apk");
                FileOutputStream fos = new FileOutputStream(file);
                InputStream inputStream = connection.getInputStream();
                int len;
                byte[] buffer = new byte[1024];
                int total = 0;
                while ((len = inputStream.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                    total += len;
                    pd.setProgress(total);
                    System.out.println(total);
                }
                fos.close();
                inputStream.close();
                return file;
            }

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


}



你可能感兴趣的:(版本更新简要)