public void download(String url, String fileName) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      Uri uri = Uri.parse(url);

      DownloadManager.Request r = new DownloadManager.Request(uri);
      r.setAllowedNetworkTypes(Request.NETWORK_WIFI);
      r.setAllowedOverRoaming(false);

      // set mime type
      MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
      String mimeType =
          mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
      r.setMimeType(mimeType);

      // set in notification
      r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
      r.setVisibleInDownloadsUi(true);

      // sdcard
      if (TextUtils.isEmpty(fileName)) fileName = uri.getLastPathSegment();
      r.setDestinationInExternalFilesDir(mContext, File.separator, fileName);
      r.setTitle(fileName);

      // start download
      mDownloadId = mDM.enqueue(r);
    } else if (mListener != null) {
      SystemClock.sleep(1000);
      mListener.onDownloadComplete(null);
    }
  }
  private static void enqueueDownload(
      Context context,
      Uri uri,
      Uri destinationUri,
      String description,
      String mimeType,
      String mediaType,
      boolean wifiOnly) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      final DownloadManager dm =
          (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
      DownloadManager.Request request = new DownloadManager.Request(uri);

      request.setDestinationUri(destinationUri);
      request.setDescription(description);
      if (mediaType != null) {
        request.addRequestHeader("Accept", mediaType);
      }
      if (mimeType != null) {
        request.setMimeType(mimeType);
      }
      if (wifiOnly) {
        restrictDownloadToWifi(request);
      }
      request.setAllowedOverRoaming(false);

      dm.enqueue(request);
    } else {
      // HACK alert:
      // Gingerbread's DownloadManager needlessly rejected HTTPS URIs. Circumvent that
      // by building and enqueing the request to the provider by ourselves. This is safe
      // as we only rely on internal API that won't change anymore.
      ContentValues values = new ContentValues();
      values.put("uri", uri.toString());
      values.put("is_public_api", true);
      values.put("notificationpackage", context.getPackageName());
      values.put("destination", 4);
      values.put("hint", destinationUri.toString());
      if (mediaType != null) {
        values.put("http_header_0", "Accept:" + mediaType);
      }
      values.put("description", description);
      if (mimeType != null) {
        values.put("mimetype", mimeType);
      }
      values.put("visibility", 0);
      values.put("allowed_network_types", wifiOnly ? DownloadManager.Request.NETWORK_WIFI : ~0);
      values.put("allow_roaming", false);
      values.put("is_visible_in_downloads_ui", true);

      context.getContentResolver().insert(Uri.parse("content://downloads/my_downloads"), values);
    }
  }
Esempio n. 3
1
  @RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  public static long DownloadApkWithProgress(Context context, String url) {

    DownloadManager downloadManager =
        (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDestinationInExternalPublicDir("/appupdate", "update.apk");
    request.setTitle("Updating: " + context.getPackageName());
    request.setMimeType(MINETYPE_APPLCATION);
    long downloadId = 0;
    // fix crash on SecurityException.
    try {
      downloadId = downloadManager.enqueue(request);
      // this register may cause memory leak if installation failed.
      context.registerReceiver(
          new DownloadCompleteReceiver(),
          new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    } catch (SecurityException e) {
      Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    return downloadId;
  }
Esempio n. 4
0
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    Uri uri =
        Uri.parse(
            moeapk.ApkDownloadUrl(
                app_package,
                moeapk.current_apklist_vcode[position],
                moeapk.current_apklist_special[position]));
    // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    // startActivity(intent);

    DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setDestinationInExternalPublicDir(
        "MoeApk",
        moeapk.current_apklist_appname[position]
            + "_"
            + moeapk.current_apklist_vname[position]
            + "_"
            + moeapk.current_apklist_sname[position]
            + ".apk");
    request.setAllowedNetworkTypes(
        DownloadManager.Request.NETWORK_MOBILE
            | DownloadManager.Request.NETWORK_WIFI); // 允许流量和wifi使用
    request.setAllowedOverRoaming(false); // 不允许在漫游时下载
    request.setMimeType("application/vnd.android.package-archive");
    request.setTitle(moeapk.current_apklist_appname[position]);
    request.setDescription("来自萌萌安卓的下载");
    request.setVisibleInDownloadsUi(true);
    long downloadid = mgr.enqueue(request);
    Toast.makeText(
            getApplicationContext(), "文件将保存在" + fileUtils.SDCARD + "MoeApk下", Toast.LENGTH_LONG)
        .show();
  }