安卓APP从服务器获取版本信息并下载安装APK

    本文地址:http://tongxinmao.com/Article/Detail/id/469

    private void checkVersion() {
          new Thread(){
             @Override
             public void run() {
                super.run();
    
                long startTime = System.currentTimeMillis();
                try {
                   String urlstr="http://ver.php?d=wificfgapp&v="+ApkUtil.getVersionCode(MainActivity.this);
                   URL url = new URL(urlstr);
                   //开启一个连接
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
                   connection.setConnectTimeout(8000);
                   connection.setReadTimeout(8000);
                   connection.setRequestMethod("GET");
                   if(connection.getResponseCode()==200){
    
                      Log.d("AA",urlstr);
                      InputStream is = connection.getInputStream();
                       InputStreamReader isReader = new InputStreamReader(is);
    
                       /*
                       BufferedReader bufferedReader = new BufferedReader(isReader);
                      StringBuffer strBuffer = new StringBuffer();
                      String line = null;
                      while (null!=(line = bufferedReader.readLine()))  {
                         strBuffer.append(line);
                      }
    
                      String result = strBuffer.toString();
                      Log.d("AA",result);
    */
                      JSONParser jsonParser = new JSONParser();
                      JSONObject jsonObject = (JSONObject)jsonParser.parse(isReader);
                      StringWriter out = new StringWriter();
                      jsonObject.writeJSONString(out);
                      Log.d("AA",out.toString());
    
                      VersionInfo ver= new VersionInfo();
                      ver.versionName = (String) jsonObject.get("versionName");
                      ver.desc = (String) jsonObject.get("desc");
                      Number num = (Long) jsonObject.get("versionCode");
                      ver.versionCode = num.intValue();
                      ver.apkUrl = (String) jsonObject.get("apkUrl");
                      ver.apkSize = (String) jsonObject.get("apkSize");
    
                      if(jsonObject.containsKey("md5"))
                      {
                         ver.md5 = (String) jsonObject.get("md5");
                      }
    
    
    
                      if(ver.versionCode>ApkUtil.getVersionCode(getApplicationContext()))
                      {
                         Message msg = Message.obtain();
                         msg.what =200;
                         msg.obj = ver;
                         handler.sendMessage(msg);
                      }
    
    
    
                   }
                } catch (IOException e) {
                   e.printStackTrace();
                }catch (ParseException e) {
                   e.printStackTrace();
                } finally {
                   long endTime = System.currentTimeMillis();
                   if(endTime-startTime<4000){
                      try {
                         Thread.sleep(4000-(endTime-startTime));
                      } catch (Exception e) {
                         e.printStackTrace();
                      }
                   }
    
                }
             }
          }.start();
       }
    
    
       private class VersionInfo
       {
    
          public  String versionName;
          public int versionCode;
          public String apkUrl;
          public String desc;
          public String apkSize;
          public String md5;
          public int forceUpdate;
    
    
       }
       private void update(VersionInfo ver) {
    
    
          /*
           * 整个库允许配置的内容
           * 非必选
           */
          UpdateConfiguration configuration = new UpdateConfiguration()
                //输出错误日志
                .setEnableLog(true)
                //设置自定义的下载
                //.setHttpManager()
                //下载完成自动跳动安装页面
                .setJumpInstallPage(true)
                //设置对话框背景图片 (图片规范参照demo中的示例图)
                //.setDialogImage(R.drawable.ic_dialog)
                //设置按钮的颜色
                //.setDialogButtonColor(Color.parseColor("#E743DA"))
                //设置对话框强制更新时进度条和文字的颜色
                //.setDialogProgressBarColor(Color.parseColor("#E743DA"))
                //设置按钮的文字颜色
                .setDialogButtonTextColor(Color.WHITE)
                //设置是否显示通知栏进度
                .setShowNotification(true)
                //设置是否提示后台下载toast
                .setShowBgdToast(false)
                //设置强制更新
                .setForcedUpgrade(ver.forceUpdate>0?true:false);
                //设置对话框按钮的点击监听
                //.setButtonClickListener(this)
                //设置下载过程的监听
                //.setOnDownloadListener(listenerAdapter);
    
          boolean b = ApkUtil.deleteOldApk(this, getExternalCacheDir().getPath() + "/autoupdate.apk");
    
            DownloadManager manager;
    
          manager = DownloadManager.getInstance(this);
          manager.setApkName("autoupdate.apk")
                .setApkUrl(ver.apkUrl)
                 .setSmallIcon(R.drawable.ic_launcher)
                .setShowNewerToast(true)
                .setConfiguration(configuration)
                .setApkVersionCode(ver.versionCode)
                .setApkVersionName(ver.versionName)
                .setApkSize(ver.apkSize)
                .setApkDescription(ver.desc)
    //                .setApkMD5("DC501F04BBAA458C9DC33008EFED5E7F")
                .download();
       }

     implementation 'com.azhon:appupdate:3.0.2'

       compile files('libs/json-simple-1.1.1.jar')


    上一篇:USB编程入门必知的8个问题
    下一篇:MFC界面库(转)