Android版本更新知识

一.版本的基础知识

        版本控制的属性包括versionCode和versionName。

(一)versionCode

        版本号(versionCode)是相对比较重要的一个属性。versionCode是一个Integer类型的值。所以大家在设置的时候,不要将versionCode设置的太大,最好不要超过Integer的取值范围(当然一般也是不会超过的),一般大家在发布自己的第一个应用到市场的时候,版本取值为1(versionCode=1),这也是目前典型和普遍的做法。然后,每次发布更新版本时可以递增versionCode的值。

(二)versionName

        版本名(versionName)一个值为String类型的属性,一般和VersionCode成对出现。VersionCode是方便程序开发者运行和维护Application而设置的一个有效的值。versionName是一个版本的描述,给用户看的,也是用户放在各个第3方平台上提供给使用者看的一个版本名,可以说是对VersionCode的解释和描述。一般格式可以为:1.1.2。(major.minor.point)的形式。

(三)版本控制小结

        版本号(versionCode)是用于判断是否升级的,一般每次版本更新,版本号加一。如果获取服务器上的版本号比检测到本程序的版本号高,那么提示升级。 
版本名(versionName)用于显示版本改变的幅度大小,比如从2.0.1改变为2.0.2可能只是修改了一个很小的debug,如果改变为2.1.0可能是新增了一些功能,如果改变为3.0.0可能是有很大幅度的修改,比如很多UI界面或功能的添加! 
        也就是版本号用于判断是否可以升级,而版本名用于显示給用户看!

(五)版本信息的获取,代码

这里指的是获取运行中的程序的版本号,代码如下: 

 /*
* 获取当前程序的版本名
*/
 private String getVersionName() throws Exception {
     //获取packagemanager的实例
     PackageManager packageManager = getPackageManager();
     //getPackageName()是你当前类的包名,0代表是获取版本信息
     PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
     Log.e("TAG", "版本号" + packInfo.versionCode);
     Log.e("TAG", "版本名" + packInfo.versionName);
     return packInfo.versionName;

 }
/*
 * 获取当前程序的版本号
 */
private int getVersionCode() {
    try {
        //获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        //getPackageName()是你当前类的包名,0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        Log.e("TAG", "版本号" + packInfo.versionCode);
        Log.e("TAG", "版本名" + packInfo.versionName);
        return packInfo.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 1;
}

(六)版本更新中重要的代码块:

1.获取本程序的版本号和版本名,上面已经有了。

2.从服务器获取到一串json数据,里面包含最新程序的版本号和版本名、新版本信息等数据,需要自己解析到达对应的数据,因为服务器的数据不一样,所以这里的代码也不写!

3.检测是否更新的代码

//对比本程序的版本号和最新程序的版本号
public void checkVersion(View view) {//按钮!
        //如果检测本程序的版本号小于服务器的版本号,那么提示用户更新
        if (getVersionCode() < serviceVersionCOde) {
            showDialogUpdate();//弹出提示版本更新的对话框

        }else{
            //否则吐司,说现在是最新的版本
            Toast.makeText(this,"当前已经是最新的版本",Toast.LENGTH_SHORT).show();

        }
}

4.弹出更新提示的对话框的代码

/**
 * 提示版本更新的对话框
 */
private void showDialogUpdate() {
    // 这里的属性可以一直设置,因为每次设置后返回的是一个builder对象
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // 设置提示框的标题
    builder.setTitle("版本升级").
            // 设置提示框的图标
                    setIcon(R.mipmap.ic_launcher).
            // 设置要显示的信息
                    setMessage("发现新版本!请及时更新").
            // 设置确定按钮
                    setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Toast.makeText(MainActivity.this, "选择确定哦", 0).show();
                    loadNewVersionProgress();//下载最新的版本程序
                }
            }).

            // 设置取消按钮,null是什么都不做,并关闭对话框
                    setNegativeButton("取消", null);

    // 生产对话框
    AlertDialog alertDialog = builder.create();
    // 显示对话框
    alertDialog.show();


}

5.下载新版本程序的代码

/**
 * 下载新版本程序,需要子线程
 */
private void loadNewVersionProgress() {
  final   String uri="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk";
    final ProgressDialog pd;    //进度条对话框
    pd = new  ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setMessage("正在下载更新");
    pd.show();
    //启动子线程下载任务
    new Thread(){
        @Override
        public void run() {
            try {
                File file = getFileFromServer(uri, pd);
                sleep(3000);
                installApk(file);
                pd.dismiss(); //结束掉进度条对话框
            } catch (Exception e) {
                //下载apk失败
                Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }}.start();
}

6.根据Uri网址获得apk文件对象的代码

/**
 * 从服务器获取apk文件的代码
 * 传入网址uri,进度条对象即可获得一个File文件
 * (要在子线程中执行哦)
 */
public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{
    //如果相等的话表示当前的sdcard挂载在手机上并且是可用的
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        URL url = new URL(uri);
        HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        //获取到文件的大小
        pd.setMax(conn.getContentLength());
        InputStream is = conn.getInputStream();
        long time= System.currentTimeMillis();//当前时间的毫秒数
        File file = new File(Environment.getExternalStorageDirectory(), time+"updata.apk");
        FileOutputStream fos = new FileOutputStream(file);
        BufferedInputStream bis = new BufferedInputStream(is);
        byte[] buffer = new byte[1024];
        int len ;
        int total=0;
        while((len =bis.read(buffer))!=-1){
            fos.write(buffer, 0, len);
            total+= len;
            //获取当前下载量
            pd.setProgress(total);
        }
        fos.close();
        bis.close();
        is.close();
        return file;
    }
    else{
        return null;
    }
}

7.安装apk文件的代码

/**
 * 安装apk
 */
protected void installApk(File file) {
    Intent intent = new Intent();
    //执行动作  
    intent.setAction(Intent.ACTION_VIEW);
    //执行的数据类型  
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    startActivity(intent);
}

完整代码

package com.example.updateDemo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Currency;

public class MainActivity extends AppCompatActivity {

    private static final int DOWN_ERROR = 505;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //在页面上显示版本信息
        TextView tv_versionName = (TextView) findViewById(R.id.tv_versionName);
        try {
            tv_versionName.setText("版本名:" + getVersionName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //检测本程序的版本,这里假设从服务器中获取到最新的版本号为3
    public void checkVersion(View view) {
            //如果检测本程序的版本号小于服务器的版本号,那么提示用户更新
            if (getVersionCode() < 3) {
                showDialogUpdate();//弹出提示版本更新的对话框

            }else{
                //否则吐司,说现在是最新的版本
                Toast.makeText(this,"当前已经是最新的版本",Toast.LENGTH_SHORT).show();

            }
    }



    /**
     * 提示版本更新的对话框
     */
    private void showDialogUpdate() {
        // 这里的属性可以一直设置,因为每次设置后返回的是一个builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 设置提示框的标题
        builder.setTitle("版本升级").
                // 设置提示框的图标
                        setIcon(R.mipmap.ic_launcher).
                // 设置要显示的信息
                        setMessage("发现新版本!请及时更新").
                // 设置确定按钮
                        setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Toast.makeText(MainActivity.this, "选择确定哦", 0).show();
                        loadNewVersionProgress();//下载最新的版本程序
                    }
                }).

                // 设置取消按钮,null是什么都不做,并关闭对话框
                        setNegativeButton("取消", null);

        // 生产对话框
        AlertDialog alertDialog = builder.create();
        // 显示对话框
        alertDialog.show();


    }

    /**
     * 下载新版本程序
     */
    private void loadNewVersionProgress() {
      final   String uri="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk";
        final ProgressDialog pd;    //进度条对话框
        pd = new  ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载更新");
        pd.show();
        //启动子线程下载任务
        new Thread(){
            @Override
            public void run() {
                try {
                    File file = getFileFromServer(uri, pd);
                    sleep(3000);
                    installApk(file);
                    pd.dismiss(); //结束掉进度条对话框
                } catch (Exception e) {
                    //下载apk失败
                    Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }}.start();
    }

    /**
     * 安装apk
     */
    protected void installApk(File file) {
        Intent intent = new Intent();
        //执行动作  
        intent.setAction(Intent.ACTION_VIEW);
        //执行的数据类型  
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        startActivity(intent);
    }

    /**
     * 从服务器获取apk文件的代码
     * 传入网址uri,进度条对象即可获得一个File文件
     * (要在子线程中执行哦)
     */
    public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{
        //如果相等的话表示当前的sdcard挂载在手机上并且是可用的
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            URL url = new URL(uri);
            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            //获取到文件的大小
            pd.setMax(conn.getContentLength());
            InputStream is = conn.getInputStream();
            long time= System.currentTimeMillis();//当前时间的毫秒数
            File file = new File(Environment.getExternalStorageDirectory(), time+"updata.apk");
            FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len ;
            int total=0;
            while((len =bis.read(buffer))!=-1){
                fos.write(buffer, 0, len);
                total+= len;
                //获取当前下载量
                pd.setProgress(total);
            }
            fos.close();
            bis.close();
            is.close();
            return file;
        }
        else{
            return null;
        }
    }


    /*
     * 获取当前程序的版本名
     */
    private String getVersionName() throws Exception {
        //获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        //getPackageName()是你当前类的包名,0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        Log.e("TAG", "版本号" + packInfo.versionCode);
        Log.e("TAG", "版本名" + packInfo.versionName);
        return packInfo.versionName;

    }


    /*
     * 获取当前程序的版本号
     */
    private int getVersionCode() {
        try {

            //获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            //getPackageName()是你当前类的包名,0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            Log.e("TAG", "版本号" + packInfo.versionCode);
            Log.e("TAG", "版本名" + packInfo.versionName);
            return packInfo.versionCode;

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

        }

        return  1;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42081816/article/details/81067483