Пример #1
1
  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);
    }
  }
 public void updateIitc(final String url) {
   final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
   request.setDescription(getString(R.string.download_description));
   request.setTitle("IITCm Update");
   request.allowScanningByMediaScanner();
   final Uri fileUri =
       Uri.parse("file://" + getExternalFilesDir(null).toString() + "/iitcUpdate.apk");
   request.setDestinationUri(fileUri);
   // remove old update file...we don't want to spam the external storage
   deleteUpdateFile();
   // get download service and enqueue file
   final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
   manager.enqueue(request);
 }
Пример #3
1
  /** @param path the absolute destination path */
  static long enqueue(DownloadManager dm, String uri, String path) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));

    /* Unfortunately, we cannot use the simpler "ExternalPublicDir"
    Android feature, because on some Samsung products, we need to
    explicitly use the "external_sd" mount instead of the built-in
    storage.  Here, we must use whatever was returned by
    FindDataPath() in LocalPath.cpp. */
    // request.setDestinationInExternalPublicDir("XCSoarData", path);
    request.setDestinationUri(Uri.fromFile(new File(path)));

    request.setAllowedOverRoaming(false);
    request.setShowRunningNotification(true);
    return dm.enqueue(request);
  }
  private void downloadApk(VersionInfo info) {
    Log.d(TAG, "download apk:" + info.url);
    DownloadManager downloadManager;
    downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    Uri uri = Uri.parse(info.url);
    DownloadManager.Request request = new Request(uri);
    File downloadDir = new File(mDownloadPath);
    if (!downloadDir.exists()) {
      downloadDir.mkdirs();
    }

    request.setTitle(mContext.getString(R.string.app_name));
    request.setDescription(info.versionName);
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
    request.setDestinationUri(Uri.fromFile(new File(mDownloadPath, info.versionCode + ".apk")));
    mDownloadReference = downloadManager.enqueue(request);
    IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    mContext.registerReceiver(mReceiver, filter);
  }
Пример #5
0
 public void downloadFile(String streamUrl, String destPath) {
   Uri url = Uri.parse(streamUrl);
   Uri destP = Uri.fromFile(new File(destPath));
   DownloadManager.Request request = new DownloadManager.Request(url);
   request.setDestinationUri(destP);
   request.setVisibleInDownloadsUi(true);
   request.setAllowedNetworkTypes(
       DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
   downloadManager.enqueue(request);
 }