Esempio n. 1
0
        public void handleMessage(Message msg) {
          switch (msg.what) {
            case DOWN_UPDATE:

              // if (msg.obj == null)
              // return;
              // String arr[] = (String[]) (msg.obj);
              // Log.d("", "wmz:" + arr[0] + ",1=" + arr[1]);
              // float current = Integer.parseInt(arr[1]);
              // float total = Integer.parseInt(arr[0]);
              // progress = (int) (current / total * 100);
              // DecimalFormat df = new DecimalFormat("0.00");
              // tmpFileSize = df.format((float) current / 1024 / 1024) +
              // "MB";
              // apkFileSize = df.format((float) total / 1024 / 1024) + "MB";
              // ��ǰ����ֵ
              Log.d("tag", "wmz:progress=" + progress);
              mProgress.setProgress(progress);
              mProgressText.setText(tmpFileSize + "/" + apkFileSize);
              break;
            case DOWN_OVER:
              downloadDialog.dismiss();
              installApk();
              break;
            case DOWN_NOSDCARD:
              downloadDialog.dismiss();
              Toast.makeText(mContext, "DOWN_NOSDCARD", 3000).show();
              break;
            case 3:
              UpdateBean bean = JsonUtils.getUpgradBeanFromJsonByCheckVersion(msg.obj.toString());
              Log.d("", "wmz:upgrad=" + bean);
              // if (canUpgrad(bean)) {
              // appUpgrad();
              // }
              mUpdateBean = bean;
              if (curVersionCode < bean.getVersionCode()) {
                apkUrl = bean.getDownloadUrl();
                updateMsg = bean.getUpdateLog();
                showNoticeDialog();
              }
              break;
          }
        };
Esempio n. 2
0
        @Override
        public void run() {
          try {
            String apkName = "OSChinaApp_" + mUpdateBean.getVersionName() + ".apk";
            String tmpApk = "OSChinaApp_" + mUpdateBean.getVersionName() + ".tmp";
            // 判断是否挂载了SD卡
            String storageState = Environment.getExternalStorageState();
            if (storageState.equals(Environment.MEDIA_MOUNTED)) {
              savePath =
                  Environment.getExternalStorageDirectory().getAbsolutePath() + "/OSChina/Update/";
              File file = new File(savePath);
              if (!file.exists()) {
                file.mkdirs();
              }
              apkFilePath = savePath + apkName;
              tmpFilePath = savePath + tmpApk;
            }

            // 没有挂载SD卡,无法下载文件
            if (apkFilePath == null || apkFilePath == "") {
              mHandler.sendEmptyMessage(DOWN_NOSDCARD);
              return;
            }

            File ApkFile = new File(apkFilePath);

            // 是否已下载更新文件
            if (ApkFile.exists()) {
              downloadDialog.dismiss();
              installApk();
              return;
            }

            // 输出临时下载文件
            File tmpFile = new File(tmpFilePath);
            FileOutputStream fos = new FileOutputStream(tmpFile);
            //				apkUrl = "https://www.oschina.net/uploads/osc-android-app-2.4.1.apk";
            URL url = new URL(apkUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            int length = conn.getContentLength();

            InputStream is = conn.getInputStream();
            int available = is.available();
            Log.d("", "wmz:available=" + available + ",length=" + length);
            // 显示文件大小格式:2个小数点显示
            DecimalFormat df = new DecimalFormat("0.00");
            // 进度条下面显示的总文件大小
            apkFileSize = df.format((float) length / 1024 / 1024) + "MB";
            Log.d("", "wmz:size=" + apkFileSize);

            int count = 0;
            byte buf[] = new byte[1024];

            do {
              int numread = is.read(buf);
              count += numread;
              // 进度条下面显示的当前下载文件大小
              tmpFileSize = df.format((float) count / 1024 / 1024) + "MB";
              // 当前进度值
              progress = (int) (((float) count / length) * 100);
              // 更新进度
              mHandler.sendEmptyMessage(DOWN_UPDATE);
              if (numread <= 0) {
                // 下载完成 - 将临时下载文件转成APK文件
                if (tmpFile.renameTo(ApkFile)) {
                  // 通知安装
                  mHandler.sendEmptyMessage(DOWN_OVER);
                }
                break;
              }
              fos.write(buf, 0, numread);
            } while (!interceptFlag); // 点击取消就停止下载

            fos.close();
            is.close();
          } catch (MalformedURLException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }