Example usage for android.content Context DOWNLOAD_SERVICE

List of usage examples for android.content Context DOWNLOAD_SERVICE

Introduction

In this page you can find the example usage for android.content Context DOWNLOAD_SERVICE.

Prototype

String DOWNLOAD_SERVICE

To view the source code for android.content Context DOWNLOAD_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.DownloadManager for requesting HTTP downloads.

Usage

From source file:com.creativtrendz.folio.ui.FolioWebViewScroll.java

/**
 * Handles a download by loading the file from `fromUrl` and saving it to `toFilename` on the external storage
 * <p/>/*from w w w . j a  v a 2 s.c om*/
 * This requires the two permissions `android.permission.INTERNET` and `android.permission.WRITE_EXTERNAL_STORAGE`
 * <p/>
 * Only supported on API level 9 (Android 2.3) and above
 *
 * @param context    a valid `Context` reference
 * @param fromUrl    the URL of the file to download, e.g. the one from `AdvancedWebView.onDownloadRequested(...)`
 * @param toFilename the name of the destination file where the download should be saved, e.g. `myImage.jpg`
 * @return whether the download has been successfully handled or not
 */
@SuppressLint("NewApi")
public static boolean handleDownload(final Context context, final String fromUrl, final String toFilename) {
    if (Build.VERSION.SDK_INT < 9) {
        throw new RuntimeException("Method requires API level 9 or above");
    }

    final Request request = new Request(Uri.parse(fromUrl));
    if (Build.VERSION.SDK_INT >= 11) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, toFilename);

    final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    try {
        try {
            dm.enqueue(request);
        } catch (SecurityException e) {
            if (Build.VERSION.SDK_INT >= 11) {
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            }
            dm.enqueue(request);
        }

        return true;
    }
    // if the download manager app has been disabled on the device
    catch (IllegalArgumentException e) {
        // show the settings screen where the user can enable the download manager app again
        openAppSettings(context, FolioWebViewScroll.PACKAGE_NAME_DOWNLOAD_MANAGER);

        return false;
    }
}

From source file:me.piebridge.bible.Bible.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void cancel(long id) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
        return;//from  w w  w . ja va2s. co m
    }
    DownloadManager dm = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    dm.remove(id);
}

From source file:com.slarker.tech.hi0734.view.widget.webview.AdvancedWebView.java

/**
 * Handles a download by loading the file from `fromUrl` and saving it to `toFilename` on the external storage
 * <p>/*w  w  w. j  a  v  a2  s . c om*/
 * This requires the two permissions `android.permission.INTERNET` and `android.permission.WRITE_EXTERNAL_STORAGE`
 * <p>
 * Only supported on API level 9 (Android 2.3) and above
 *
 * @param context    a valid `Context` reference
 * @param fromUrl    the URL of the file to download, e.g. the one from `AdvancedWebView.onDownloadRequested(...)`
 * @param toFilename the name of the destination file where the download should be saved, e.g. `myImage.jpg`
 * @return whether the download has been successfully handled or not
 */
@SuppressLint("NewApi")
public static boolean handleDownload(final Context context, final String fromUrl, final String toFilename) {
    if (Build.VERSION.SDK_INT < 9) {
        throw new RuntimeException("Method requires API level 9 or above");
    }

    final Request request = new Request(Uri.parse(fromUrl));
    if (Build.VERSION.SDK_INT >= 11) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, toFilename);

    final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    try {
        try {
            dm.enqueue(request);
        } catch (SecurityException e) {
            if (Build.VERSION.SDK_INT >= 11) {
                request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
            }
            dm.enqueue(request);
        }

        return true;
    }
    // if the download manager app has been disabled on the device
    catch (IllegalArgumentException e) {
        // show the settings screen where the user can enable the download manager app again
        openAppSettings(context, AdvancedWebView.PACKAGE_NAME_DOWNLOAD_MANAGER);

        return false;
    }
}

From source file:org.mozilla.gecko.GeckoApp.java

void handleDownloadDone(String displayName, String path, String mimeType, int size) {
    // DownloadManager.addCompletedDownload is supported in level 12 and higher
    if (Build.VERSION.SDK_INT >= 12) {
        DownloadManager dm = (DownloadManager) mAppContext.getSystemService(Context.DOWNLOAD_SERVICE);
        dm.addCompletedDownload(displayName, displayName, false /* do not use media scanner */, mimeType, path,
                size, false /* no notification */);
    }//from   ww w . j a v a 2  s .c o  m
}

From source file:im.neon.activity.CommonActivityUtils.java

/**
 * Save a media URI into the download directory
 *
 * @param context  the context//from w  w w . jav a2 s.c  o  m
 * @param srcFile  the source file.
 * @param filename the filename (optional)
 * @return the downloads file path
 */
@SuppressLint("NewApi")
public static String saveMediaIntoDownloads(Context context, File srcFile, String filename, String mimeType) {
    String fullFilePath = saveFileInto(srcFile, Environment.DIRECTORY_DOWNLOADS, filename);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (null != fullFilePath) {
            DownloadManager downloadManager = (DownloadManager) context
                    .getSystemService(Context.DOWNLOAD_SERVICE);

            try {
                File file = new File(fullFilePath);
                downloadManager.addCompletedDownload(file.getName(), file.getName(), true, mimeType,
                        file.getAbsolutePath(), file.length(), true);
            } catch (Exception e) {
                Log.e(LOG_TAG, "## saveMediaIntoDownloads(): Exception Msg=" + e.getMessage());
            }
        }
    }

    return fullFilePath;
}