实现版本更新

来源:互联网 发布:java 定义object数组 编辑:程序博客网 时间:2024/05/29 18:53

 


private String url = "http://125.39.134.47/r/a.gdown.baidu.com/data/wisegame/7c28ac069399b336/kuaishou_4812.apk";
    private ProgressDialog progressDialog;//加载框
    private Callback.Cancelable cancelable;//请求任务对象

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

        initDialog();
    }

//第一步:拿到本app的版本号

     

PackageManager manager = getPackageManager();PackageInfo info = manager.getPackageInfo(getPackageName(), 0);int versionCode = info.versionCode;


 //第二步:进行网络请求,请求版本对象信息(解析json字符串转化为本地version对象)

       

Version version=new Version();version.setUrl(url);


//第三步:比较,如果服务器版本号大于本app版本号,那么下载文件,下载完成后,进行安装

if(versionCode<version.getVersionCode()){    file = new File(Config.VERSION_PATH);    //下载apk    downloadApk();}/*** 实现下载进度的方法*/
private void initDialog() {    progressDialog = new ProgressDialog(this);    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);    progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialogInterface, int i) {            cancelable.cancel();        }    });}

/*
*下载apk的方法
*
*/

private void downloadApk() {    RequestParams params=new RequestParams(url);    params.setAutoRename(true);    params.setCancelFast(true);    //判断sd卡是否可用    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))    {        params.setSaveFilePath(Config.VERSION_PATH);    }    cancelable= x.http().get(params, new Callback.ProgressCallback<File>() {        @Override        public void onSuccess(File result) {            progressDialog.dismiss();            System.out.println("filepath====" + result.getAbsolutePath());            install(result);        }        @Override        public void onError(Throwable ex, boolean isOnCallback) {        }        @Override        public void onCancelled(CancelledException cex) {        }        @Override        public void onFinished() {        }        @Override        public void onWaiting() {        }        @Override        public void onStarted() {            progressDialog.show();        }        @Override        public void onLoading(long total, long current, boolean isDownloading) {            if(isDownloading)            {                progressDialog.setMax((int) total);                progressDialog.setProgress((int) current);                progressDialog.setTitle("下载进度");            }        }    });}/** *安装应用的方法 */
private void install(File result) {    Intent intent = new Intent(Intent.ACTION_VIEW);    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive");    startActivity(intent);}新建的bean的对象config对象存放路径public class Config {    //apk路径    public static final String VERSION_PATH = Environment.getExternalStorageDirectory()+"/kson/versions.apk";}

Version对象 存放版本号和url
public class Version {
    private int VersionCode = 200;//版本号
    private String url;//远程apk地址

    public int getVersionCode() {
        return VersionCode;
    }

    public void setVersionCode(int versionCode) {
        VersionCode = versionCode;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}