工具代码---版本更新

第一步获取当前版本号

public static  String getVersionNow(Context con){
        PackageManager packageManager = con.getPackageManager();        
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(con.getPackageName(),0);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        return packInfo.versionName;
    }
  • -

第二步 获取最新版本号

第三步 弹出对话框提示下载更新

public static void AlertDialog(Context con,String versionName, String versionName_new,final DialogPosNav dia) {
        final android.app.AlertDialog.Builder b=new Builder(con);
        b.setTitle("版本更新");
        b.setMessage("当前版本是"+versionName+" 最新版本是"+versionName_new+"确定要更新吗?");


        b.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                android.app.AlertDialog dialog = b.create();
                //downLoadApk();  
                dialog.dismiss();
                dia.positive(); //接口回调 更新接口回调
            }



        });

        b.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                android.app.AlertDialog dialog = b.create();
                dialog.dismiss();
                //positive();
                dia.negative();//接口回调 不更新接口回调
            }

        });

        b.show();


    }


//定义一个接口 用于点击事件方法的回调
        public interface DialogPosNav{
            public void positive();
            public void negative();

        }

第四步 下载

public static void downLoadApk(final Context con,String url) {
        pd = new ProgressDialog(con);
        pd.setTitle("正在下载更新");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
        final String uu=url.trim();
        System.out.println("下载的地址-------------------------"+uu);
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    File file = getFileFromServer(con,uu,pd);//http://download.mgsit.com.cn/download/multigold15.04.02.apk
                    Thread.sleep(3000);
                    pd.dismiss();
                    if(file!=null){
                        installAPK(file,con);
                    }else{

                    }


                } catch (InterruptedException e) {


                    ToastUtils.Toas(con, "下载失败,请下次再试");
                    e.printStackTrace();
                }

            }
        }).start();

    }


/**
     * 从服务器获取更新的文件
     * @param path 路径
     * @param pd 进度条
     */
    protected static File getFileFromServer(Context con,String path, ProgressDialog pd) {

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //sd卡正常
            try {
                URL url = new URL(path);
                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                pd.setMax(conn.getContentLength());
                InputStream in=conn.getInputStream();
                File file_apk=new File(Environment.getExternalStorageDirectory(),"updata.apk");
                FileOutputStream out=new FileOutputStream(file_apk);
                BufferedInputStream bs=new BufferedInputStream(in);
                byte[] bt=new byte[1024];
                int total=0;
                int len;
                while((len=bs.read(bt))!=-1){
                    out.write(bt, 0, len);
                    total+=len;
                    pd.setProgress(total);
                }
                out.close();
                bs.close();
                in.close();

                return file_apk;
            } catch (Exception e) {

                ToastUtils.Toas(con, "下载新版本出错 请稍后再试");
                e.printStackTrace();
            }


        }else{


            System.out.println("sd卡不正常");
            ToastUtils.Toas(con, "sd卡不可用,请稍后再试");
            return null;
        }

        return null;

    }


    /**
     * 安装apk
     * @param file
     */
    protected static void installAPK(File file,Context con) {

            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");  
            con.startActivity(intent);


    }

第五步 使用

VersionUtils.AlertDialog(Activity_Setting.this, versionNow, versonNet, new DialogPosNav() {

            @Override
            public void positive() {                                 //工具类
                VersionUtils.downLoadApk(Activity_Setting.this,   "url");

            }

            @Override
            public void negative() {


            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值