Android - DownloadManager的使用

jopen 8年前發布 | 8K 次閱讀 Android開發 移動開發

DownloadManager是Android的基礎類, 用于下載文件, 完成自動更新的功能.
使用方式

DownloadUtil downloadUtil = new DownloadUtil(context);
downloadUtil.download();

代碼

package me.chunyu.model.utils;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.webkit.MimeTypeMap;

/** * 下載類, 默認下載春雨醫生APK, 也可以下載其他APK. * 存儲位置: /sdcard/download/filename * <p/> * Created by C.L.Wang on 15/7/27. */
@TargetApi(11)
public class DownloadUtil {

    private static final String CHUNYU_DOWNLOAD_URL =
            "http://www.chunyuyisheng.com/download/chunyu/latest/";

    private static final String APK_TYPE =
            "application/vnd.android.package-archive";

    private static final String DEFAULT_FILE_NAME = "chunyudoctor.apk";

    private static final String DEFAULT_TITLE = "春雨醫生";

    private Context mContext; // 下載進行
    private String mUrl; // URL地址
    private String mFileName; // 文件名
    private String mTitle; // 通知欄標題

    private DownloadManager mDownloadManager; // 下載管理器
    private long mDownloadId; // 下載ID

    // 下載完成的接收器
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            if (downloadId == mDownloadId) {
                installApp(mDownloadId);
                destroyArgs();
            }
        }
    };

    /** * 默認構造器, 下載春雨醫生APK * * @param context 上下文 */
    public DownloadUtil(Context context) {
        this(context, CHUNYU_DOWNLOAD_URL, DEFAULT_FILE_NAME, DEFAULT_TITLE);
    }

    /** * 參數構造器, 下載其他文件 * * @param context 活動 * @param url URL * @param fileName 存儲文件名 * @param title 通知欄標題 */
    public DownloadUtil(Context context, String url, String fileName, String title) {
        mContext = context;
        mUrl = url;
        mFileName = fileName;
        mTitle = title;
        initArgs();
    }

    /** * 下載文件 */
    public void download() {
        // 設置下載Url
        Uri resource = Uri.parse(mUrl);
        DownloadManager.Request request = new DownloadManager.Request(resource);

        // 設置文件類型
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUrl));
        request.setMimeType(mimeString);

        // 下載完成時在進度條提示
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        // 存儲sdcard目錄的download文件夾
        request.setDestinationInExternalPublicDir("/download/", mFileName);
        request.setTitle(mTitle);

        // 開始下載
        mDownloadId = mDownloadManager.enqueue(request);
    }

    // 初始化
    private void initArgs() {
        mDownloadManager = (DownloadManager) mContext.getSystemService((Context.DOWNLOAD_SERVICE));
        mContext.registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    // 析構
    private void destroyArgs() {
        mContext.unregisterReceiver(mReceiver);
    }

    // 安裝App
    private void installApp(long downloadId) {
        Intent install = new Intent(Intent.ACTION_VIEW);
        Uri downloadFileUri = mDownloadManager.getUriForDownloadedFile(downloadId);
        install.setDataAndType(downloadFileUri, APK_TYPE);
        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(install);
    }

}

來自: http://blog.csdn.net//caroline_wendy/article/details/47089019

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!