版本下载和更新

本文介绍了一种Android应用检查远程服务器上的版本更新并下载新版本的方法。通过解析JSON数据来获取最新的APK链接和版本号,然后使用对话框提示用户进行更新。文章详细展示了如何实现版本检查、下载及安装流程。
摘要由CSDN通过智能技术生成
下载 ( 版本更新 )
private void uploadApk() {
    RequestParams params = new RequestParams("http://mapp.qzone.qq.com/cgi-bin/mapp/mapp_subcatelist_qq?yyb_cateid=-10&categoryName=%E8%85%BE%E8%AE%AF%E8%BD%AF%E4%BB%B6&pageNo=1&pageSize=20&type=app&platform=touch&network_type=unknown&resolution=412x732");
    x. http ().get(params, new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            Log. i ("xxx", result.toString());
            try {
      JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("app");
                JSONObject jo = jsonArray.getJSONObject(0);//这个是获取的第0个位置, 可以循环遍历获取不同的apk的对象
         //url  apk地址
                String url = jo.getString("url");
                String version = jo.getString("versionName");
                Log. i ("xxx", "url:" + url + ",versionName:" + version);
                     String versionName = getVersionName();
                 // 判断 versionName
                /*if (version.compareTo(versionName) > 0) {
                    showChoiseDialog(url);
                }*/
                showChoiseDialog(url);

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

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    });

**
 * 获取版本名称
 */
private String getVersionName() {
    // 获取packagemanager的实例
    PackageManager packageManager = getPackageManager();
    // getPackageName()是你当前类的包名,0代表是获取版本信息
    PackageInfo info = null;
    try {
        info = packageManager.getPackageInfo(getPackageName(), 0);

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    String versionName = info.versionName;
    return versionName;
}

/**
 * 获取版本号
 *
 * @return
  * @throws Exception
 */
public String getVersionCode() {
    // 获取packagemanager的实例
    PackageManager packageManager = getPackageManager();
    // getPackageName()是你当前类的包名,0代表是获取版本信息
    PackageInfo packInfo = null;
    try {
        packInfo = packageManager.getPackageInfo(getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    String versionCode = String. valueOf (packInfo.versionCode);
    return versionCode;
}
 
/**
 * 显示更新选择对话框
 *
 * @param url
 */
private void showChoiseDialog(final String url) {


   android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
    android.app.AlertDialog dialog = null;

    builder.setTitle("版本更新");
    builder.setMessage("检测到新版本,是否下载更新?");
    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
                    //下载
               downLoadApk(url);

        }
    });
    builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}
 
 
private void downLoadApk(url) {
 
   RequestParams params = new RequestParams(url);
    //自定义保存路径 Environment.getExternalStorageDirectory() sdcard 根目录

    params.setSaveFilePath(Environment. getExternalStorageDirectory () + "/app/");
    //自动为文件命令
    params.setAutoRename(true);
    x. http ().post(params, new Callback.ProgressCallback<File>() {

        //网络请求成功时回调
        @Override
        public void onSuccess(File result) {
            Toast. makeText (getActivity(), "下载成功", Toast. LENGTH_SHORT ).show();
            //apk下载完成后 调用系统的安装方法
            Intent intent = new Intent(Intent. ACTION_VIEW );
            intent.setDataAndType(Uri. fromFile (result), "application/vnd.android.package-archive");
            getActivity().startActivity(intent);
            Toast. makeText (getActivity(), "安装成功", Toast. LENGTH_SHORT ).show();

        }

        //网络请求错误时回调
        @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() {

        }

        //下载的时候不断回调的方法
        @Override
        public void onLoading(long total, long current, boolean isDownloading) {
            //文件总大小和当前进度
            Log. i ("xxx", total + "," + current);

        }
    });

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值