Example usage for android.app DownloadManager enqueue

List of usage examples for android.app DownloadManager enqueue

Introduction

In this page you can find the example usage for android.app DownloadManager enqueue.

Prototype

public long enqueue(Request request) 

Source Link

Document

Enqueue a new download.

Usage

From source file:Main.java

public static void downloadFile(Context context, String fileurl) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileurl));
    request.setDestinationInExternalPublicDir("/Download/", fileurl.substring(fileurl.lastIndexOf("/") + 1));
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    downloadManager.enqueue(request);
}

From source file:Main.java

/**
 * Note: Make sure isDownloadManagerAvailable return is true before use this method.
 * @param apkName Apk File Name// ww w  .  j  a  va  2s . com
 * @param fullApkUrl url of full
 * @param context Context
 */
public static void downloadApkByDownloadManager(String apkName, String fullApkUrl, Context context) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fullApkUrl));
    request.setDescription(fullApkUrl);
    request.setTitle(apkName);

    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkName);
    request.setVisibleInDownloadsUi(false);
    request.setMimeType("application/vnd.android.package-archive");

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

From source file:ru.gkpromtech.exhibition.organizations.OrganizationFilesDownloader.java

private static void download(Context context, String url, Organization organization) {
    String dir = SharedData.EXTERNAL_DIR + "army2016/";
    String file = url.substring(url.lastIndexOf('/') + 1);

    //noinspection ResultOfMethodCallIgnored
    new File(dir).mkdirs();

    DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
    req.setTitle(organization.fullname);
    req.setDescription(context.getString(R.string.materials));
    req.allowScanningByMediaScanner();/* ww  w.jav a 2s  . co  m*/
    req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    req.setDestinationUri(Uri.parse("file://" + dir + file));

    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(req);
}

From source file:Main.java

public static Long startDownload(DownloadManager dm, Handler handler, String location) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(location));
    Long id = dm.enqueue(request);
    handler.sendMessageDelayed(handler.obtainMessage(MSG_DOWNLOAD_TIMEOUT, id), DOWNLOAD_TIMEOUT_MILLIS);
    if (DEBUG)//  ww w.  jav a  2  s . co m
        Log.d(TAG, "Starting download: DownloadId=" + id);
    return id;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static long startInvisibleDownload(final Context context, final String url, final String title) {
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    String name = String.valueOf(new Random().nextInt());

    LogHandler.log(Log.WARN, "load invisible " + url);

    long id = manager.enqueue(new DownloadManager.Request(Uri.parse(url)).setTitle(title));
    Database.getInstance(context).addDownload(name, id, null);

    return id;/*from   w  w w.j ava 2s  .  c o  m*/
}

From source file:com.btmura.android.reddit.app.MenuHelper.java

public static void downloadUrl(Context context, String title, String url) {
    context = context.getApplicationContext();
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Request request = new Request(Uri.parse(url));
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setTitle(title);/*from w w  w .  j a va2s. c  o  m*/
    manager.enqueue(request);
}

From source file:com.android.browser.DownloadHandler.java

private static void onDownloadNoStreamImpl(Activity activity, String url, String userAgent,
        String contentDisposition, String mimetype, String referer, boolean privateBrowsing) {

    String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);

    // Check to see if we have an SDCard
    String status = Environment.getExternalStorageState();
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        int title;
        String msg;//from   w  ww.  ja v  a  2s  .  c o m

        // Check to see if the SDCard is busy, same as the music app
        if (status.equals(Environment.MEDIA_SHARED)) {
            msg = activity.getString(R.string.download_sdcard_busy_dlg_msg);
            title = R.string.download_sdcard_busy_dlg_title;
        } else {
            msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename);
            title = R.string.download_no_sdcard_dlg_title;
        }

        new AlertDialog.Builder(activity).setTitle(title).setIconAttribute(android.R.attr.alertDialogIcon)
                .setMessage(msg).setPositiveButton(R.string.ok, null).show();
        return;
    }

    // java.net.URI is a lot stricter than KURL so we have to encode some
    // extra characters. Fix for b 2538060 and b 1634719
    WebAddress webAddress;
    try {
        webAddress = new WebAddress(url);
        webAddress.setPath(encodePath(webAddress.getPath()));
    } catch (Exception e) {
        // This only happens for very bad urls, we want to chatch the
        // exception here
        Log.e(LOGTAG, "Exception trying to parse url:" + url);
        return;
    }

    String addressString = webAddress.toString();
    Uri uri = Uri.parse(addressString);
    final DownloadManager.Request request;
    try {
        request = new DownloadManager.Request(uri);
    } catch (IllegalArgumentException e) {
        Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show();
        return;
    }
    request.setMimeType(mimetype);
    // set downloaded file destination to /sdcard/Download.
    // or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype?
    try {
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    } catch (IllegalStateException ex) {
        // This only happens when directory Downloads can't be created or it isn't a directory
        // this is most commonly due to temporary problems with sdcard so show appropriate string
        Log.w(LOGTAG, "Exception trying to create Download dir:", ex);
        Toast.makeText(activity, R.string.download_sdcard_busy_dlg_title, Toast.LENGTH_SHORT).show();
        return;
    }
    // let this downloaded file be scanned by MediaScanner - so that it can
    // show up in Gallery app, for example.
    request.allowScanningByMediaScanner();
    request.setDescription(webAddress.getHost());
    // XXX: Have to use the old url since the cookies were stored using the
    // old percent-encoded url.
    String cookies = CookieManager.getInstance().getCookie(url, privateBrowsing);
    request.addRequestHeader("cookie", cookies);
    request.addRequestHeader("User-Agent", userAgent);
    if (!TextUtils.isEmpty(referer)) {
        request.addRequestHeader("Referer", referer);
    }
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (mimetype == null) {
        if (TextUtils.isEmpty(addressString)) {
            return;
        }
        // We must have long pressed on a link or image to download it. We
        // are not sure of the mimetype in this case, so do a head request
        new FetchUrlMimeType(activity, request, addressString, cookies, userAgent).start();
    } else {
        final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        new Thread("Browser download") {
            public void run() {
                manager.enqueue(request);
            }
        }.start();
    }
    Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT).show();
}

From source file:com.sabaibrowser.DownloadHandler.java

private static void onDownloadNoStreamImpl(Activity activity, String url, String userAgent,
        String contentDisposition, String mimetype, String referer, boolean privateBrowsing) {

    String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);

    // Check to see if we have an SDCard
    String status = Environment.getExternalStorageState();
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        int title;
        String msg;//from ww w.j av a2  s . c om

        // Check to see if the SDCard is busy, same as the music app
        if (status.equals(Environment.MEDIA_SHARED)) {
            msg = activity.getString(R.string.download_sdcard_busy_dlg_msg);
            title = R.string.download_sdcard_busy_dlg_title;
        } else {
            msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename);
            title = R.string.download_no_sdcard_dlg_title;
        }

        new AlertDialog.Builder(activity).setTitle(title).setIconAttribute(android.R.attr.alertDialogIcon)
                .setMessage(msg).setPositiveButton(R.string.ok, null).show();
        return;
    }

    // java.net.URI is a lot stricter than KURL so we have to encode some
    // extra characters. Fix for b 2538060 and b 1634719
    WebAddress webAddress;
    try {
        webAddress = new WebAddress(url);
        webAddress.setPath(encodePath(webAddress.getPath()));
    } catch (Exception e) {
        // This only happens for very bad urls, we want to chatch the
        // exception here
        Log.e(LOGTAG, "Exception trying to parse url:" + url);
        return;
    }

    String addressString = webAddress.toString();
    Uri uri = Uri.parse(addressString);
    final DownloadManager.Request request;
    try {
        request = new DownloadManager.Request(uri);
    } catch (IllegalArgumentException e) {
        Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show();
        return;
    }
    request.setMimeType(mimetype);
    // set downloaded file destination to /sdcard/Download.
    // or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype?
    try {
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    } catch (IllegalStateException ex) {
        // This only happens when directory Downloads can't be created or it isn't a directory
        // this is most commonly due to temporary problems with sdcard so show appropriate string
        Log.w(LOGTAG, "Exception trying to create Download dir:", ex);
        Toast.makeText(activity, R.string.download_sdcard_busy_dlg_title, Toast.LENGTH_SHORT).show();
        return;
    }
    // let this downloaded file be scanned by MediaScanner - so that it can
    // show up in Gallery app, for example.
    request.allowScanningByMediaScanner();
    request.setDescription(webAddress.getHost());
    // XXX: Have to use the old url since the cookies were stored using the
    // old percent-encoded url.
    String cookies = privateBrowsing ? "" : CookieManager.getInstance().getCookie(url);
    request.addRequestHeader("cookie", cookies);
    request.addRequestHeader("User-Agent", userAgent);
    if (!TextUtils.isEmpty(referer)) {
        request.addRequestHeader("Referer", referer);
    }
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (mimetype == null) {
        if (TextUtils.isEmpty(addressString)) {
            return;
        }
        // We must have long pressed on a link or image to download it. We
        // are not sure of the mimetype in this case, so do a head request
        new FetchUrlMimeType(activity, request, addressString, cookies, userAgent).start();
    } else {
        final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        new Thread("Browser download") {
            public void run() {
                manager.enqueue(request);
            }
        }.start();
    }
    Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT).show();
}

From source file:com.android.stockbrowser.DownloadHandler.java

private static void onDownloadNoStreamImpl(Activity activity, String url, String userAgent,
        String contentDisposition, String mimetype, String referer, boolean privateBrowsing) {

    String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);

    // Check to see if we have an SDCard
    String status = Environment.getExternalStorageState();
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        int title;
        String msg;//ww  w.  j a  v a  2s. c  o m

        // Check to see if the SDCard is busy, same as the music app
        if (status.equals(Environment.MEDIA_SHARED)) {
            msg = activity.getString(R.string.download_sdcard_busy_dlg_msg);
            title = R.string.download_sdcard_busy_dlg_title;
        } else {
            msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename);
            title = R.string.download_no_sdcard_dlg_title;
        }

        new AlertDialog.Builder(activity).setTitle(title).setIconAttribute(android.R.attr.alertDialogIcon)
                .setMessage(msg).setPositiveButton(R.string.ok, null).show();
        return;
    }

    // java.net.URI is a lot stricter than KURL so we have to encode some
    // extra characters. Fix for b 2538060 and b 1634719
    WebAddress webAddress;
    try {
        webAddress = new WebAddress(url);
        webAddress.setPath(encodePath(webAddress.getPath()));
    } catch (Exception e) {
        // This only happens for very bad urls, we want to chatch the
        // exception here
        Log.e(LOGTAG, "Exception trying to parse url:" + url);
        return;
    }

    String addressString = webAddress.toString();
    Uri uri = Uri.parse(addressString);
    final DownloadManager.Request request;
    try {
        request = new DownloadManager.Request(uri);
    } catch (IllegalArgumentException e) {
        Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show();
        return;
    }
    request.setMimeType(mimetype);
    // set downloaded file destination to /sdcard/Download.
    // or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype?
    try {
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
    } catch (IllegalStateException ex) {
        // This only happens when directory Downloads can't be created or it isn't a directory
        // this is most commonly due to temporary problems with sdcard so show appropriate string
        Log.w(LOGTAG, "Exception trying to create Download dir:", ex);
        Toast.makeText(activity, R.string.download_sdcard_busy_dlg_title, Toast.LENGTH_SHORT).show();
        return;
    }
    // let this downloaded file be scanned by MediaScanner - so that it can
    // show up in Gallery app, for example.
    request.allowScanningByMediaScanner();
    request.setDescription(webAddress.getHost());
    // XXX: Have to use the old url since the cookies were stored using the
    // old percent-encoded url.
    String cookies = "";//CookieManager.getInstance().getCookie(url, privateBrowsing);
    request.addRequestHeader("cookie", cookies);
    request.addRequestHeader("User-Agent", userAgent);
    if (!TextUtils.isEmpty(referer)) {
        request.addRequestHeader("Referer", referer);
    }
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (mimetype == null) {
        if (TextUtils.isEmpty(addressString)) {
            return;
        }
        // We must have long pressed on a link or image to download it. We
        // are not sure of the mimetype in this case, so do a head request
        new FetchUrlMimeType(activity, request, addressString, cookies, userAgent).start();
    } else {
        final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        new Thread("StockBrowser download") {
            public void run() {
                manager.enqueue(request);
            }
        }.start();
    }
    Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT).show();
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static long startDownload(@NonNull final Context context, @NonNull final String signature,
        @NonNull final String responseData, @NonNull final String title, @Nullable final String mimeType) {
    final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    String name;/*from w ww .  ja v a  2 s .c o  m*/

    try {
        name = new JSONObject(responseData).getString("productId");
    } catch (JSONException e) {
        LogHandler.log(e);
        return -1;
    }

    LogHandler.log(Log.WARN, "load " + name);

    long id = manager
            .enqueue(new DownloadManager.Request(Uri.parse(context.getString(R.string.product_data_url)))
                    .addRequestHeader("App-Signature", signature)
                    .addRequestHeader("App-ResponseData", responseData).setTitle(title)
                    .setDescription(context.getString(R.string.app_title)));
    Database.getInstance(context).addDownload(name, id, mimeType);

    return id;
}