Android使用HttpUtils框架首页检查更新

#Android使用HttpUtils框架首页检查更新

检查本地Aapplication版本信息

/**
 * 获取本地版本号和版本信息
 */
public static String getVersionName(Context context) {
  String versionName = "";
  try {
       ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
       versionName=context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
   } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return versionName;
}

public static int getVersionCode(Context context) {
   int versionCode = 0;
   try {
       versionCode=context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionCode;
   } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
   }
    return versionCode;
}

###导入HttpUtils的jar包
下载Xutils.jar

流程图:

Created with Raphaël 2.3.0 检查版本 有新版本 下载? 开始下载 yes no

###HttpUtils请求服务器版本信息

 //检查版本号
    public void checkVersion() {
        if (httpUtils == null)
            httpUtils = new HttpUtils(2000);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constants.HOST + "/update.txt", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //String json = responseInfo.result;
                //处理响应结果
                handleResponse(responseInfo.result);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                e.printStackTrace();
                ToastUtils.showSafeToast(SplashActivity.this, "网络异常,请检查你的网络。。。");
                turnToMain();
            }
        });
    }

    //处理请求结果
    public void handleResponse(String json) {
        UpdateInfo updateInfo = new UpdateInfo();
        try {
            //获取更新信息
            JSONObject jsonObject = new JSONObject(json);
            updateInfo.msg = jsonObject.getString("msg");
            updateInfo.version = jsonObject.getInt("version");
            updateInfo.versionName = jsonObject.getString("versionName");
            updateInfo.downloadUrl = jsonObject.getString("downloadUrl");
            int localVersion = PackageUtils.getVersionCode(SplashActivity.this);
            if (localVersion < updateInfo.version) {
                //旧版本,是否更新 对话框提示
                showDialog(updateInfo.msg, Constants.HOST + updateInfo.downloadUrl);
            } else {
                //新版本 跳转到主页面
                turnToMain();
            }
        } catch (Exception e) {
            e.printStackTrace();
            turnToMain();
        }
    }

    //下载apk
    private void downloadApk(String downloadUrl) {
        if (httpUtils == null)
            httpUtils = new HttpUtils(2000);
        final ProgressDialog pd = new ProgressDialog(SplashActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("下载中...");
        String target = Environment.getDataDirectory().getAbsolutePath() + "/new.apk";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            target = Environment.getExternalStorageDirectory().getAbsolutePath() + "/new.apk";
        }
        final String apkPath = target;
        boolean autoResume = false;//true:断点续传 如果下载完成再次下载会进入onFailure方法
        httpUtils.download(downloadUrl, target, autoResume, new RequestCallBack<File>() {
            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {
                //下载完成 掉用系统安装界面
                installApk(apkPath);
                pd.dismiss();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                //下载失败
                ToastUtils.showSafeToast(SplashActivity.this, "下载失败");
                pd.dismiss();
                turnToMain();
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                pd.setMax((int) total);
                int percent = (int) (current * 100f / total);
                pd.setProgress(percent);
            }
        });
    }

###调用系统安装器安装apk

 private void installApk(String path) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_INSTALL_PACKAGE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String type = "application/vnd.android.package-archive";
        intent.setDataAndType(Uri.parse("file://" + path), type);
        //如果用户安装不用处理 如果用户不安装 跳转到主页面
        startActivityForResult(intent, 0);
   }

注解:[^note]:第一次写,不好之处还请各位指正,欢迎各位大神一起讨论交流与。欢迎界面以后可改用ViewPager,至于网络请求框架很多,LZ这里就不多说了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值