android关于版本更新

 

public class MainActivity extends Activity {

    private Button button;
    // 更新版本接口

 

    private String apkUrl = "http://m.yunifang.com/yunifang/mobile/version?random=67603&encode=e441af3089cbeeb6b58ac72393a66667&clientType=android&clientVersion=10000";

//下载保存的地址

 

    private String sdPath = "/sdcard/" + System.currentTimeMillis() + "我的更新.apk";
    private String version;
    private String version_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        // 获得本应用的版本号
        initdata();
        /// 解析版本更新接口
        versonData();

    }

 

 

 

  

 

    // 解析版本更新接口
    public void versonData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.GET, apkUrl, new RequestCallBack<String>() {

            private VersonBean bean;

            @Override
            public void onFailure(HttpException arg0, String arg1) {

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String string = arg0.result;
                Gson gson = new Gson();
                bean = gson.fromJson(string, VersonBean.class);
                ifVersion(bean);

            }

        });

    };

    // 判断是否更新
    private void ifVersion(VersonBean bean) {
        String version_name = bean.data.version_Name;
        version_url = bean.data.url;

        if (!version_name.equals(version)) {
            Toast.makeText(MainActivity.this, "要更新了", 0).show();

            AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("软件版本更新");
            dialog.setMessage(bean.data.description);
            dialog.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // 点击立即下载更新包
                    downLoadApk();
                    dialog.dismiss();
                }

            });
            dialog.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            AlertDialog alertDialog = dialog.create();
            alertDialog.show();

        } else {
            Toast.makeText(MainActivity.this, "不更新", 0).show();
        }

    }

    private void initdata() {
        // 获取本应用的版本号
        PackageManager manager = this.getPackageManager();
        PackageInfo info;
        try {
            info = manager.getPackageInfo(this.getPackageName(), 0);
            version = info.versionName;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    // 下载新版apk

 

    private void downLoadApk() {

     pd = new ProgressDialog(MainActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载更新");

 

        HttpUtils httpUtils = new HttpUtils();

        httpUtils.download(version_url, sdPath, new RequestCallBack<File>() {

            @Override
            public void onSuccess(ResponseInfo<File> arg0) {
                //获得apk安装地址
                String path = arg0.result.getPath();
                File file = new File(path);
                //用隐式跳转进行安装
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                startActivity(intent);
            }

            
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                // TODO Auto-generated method stub

 

                super.onLoading(total, current, isUploading);

                 pd.setMax(( int)total);
                  pd.setProgress(( int)current);

 

 

            }


            @Override
            public void onStart() {
                pd.show();
                super.onStart();
            }


            @Override
            public void onFailure(HttpException arg0, String arg1) {

            }
        });

    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值