예제 #1
0
 private void afertDownload() {
   String suff = FilenameUtils.getExtension(downloadUrl).trim().toLowerCase();
   String type;
   File f = new File(savePath, FilenameUtils.getName(downloadUrl));
   if (!f.exists()) {
     return;
   }
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   if ("apk".equals(suff)) {
     type = "application/vnd.android.package-archive";
   } else {
     type = "application/msword";
   }
   intent.setDataAndType(Uri.fromFile(f), type);
   mContext.startActivity(intent);
 }
예제 #2
0
    @Override
    public void run() {
      try {
        URL url = new URL(downloadUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();

        int length = conn.getContentLength();
        InputStream is = conn.getInputStream();
        File file = new File(savePath);
        if (!file.exists()) {
          file.mkdir();
        }
        File apkFile = new File(savePath, FilenameUtils.getName(downloadUrl));
        FileOutputStream fos = new FileOutputStream(apkFile);
        int count = 0;
        byte buf[] = new byte[1024];
        do {
          int numread = is.read(buf);
          count += numread;
          progress = (int) (((float) count / length) * 100);
          mHandler.sendEmptyMessage(DOWNLOAD);
          if (numread <= 0) {
            mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
            break;
          }
          fos.write(buf, 0, numread);
        } while (!cancelUpdate);
        fos.close();
        is.close();
      } catch (Exception e) {
        e.printStackTrace();
        if (mDownloadDialog.isShowing()) {
          mDownloadDialog.dismiss();
        }
      }
    }