package com.anyun.exam.lib; import android.app.DownloadManager; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.util.Log; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DownloadManagerUtil { public static final String TAG = DownloadManagerUtil.class.getCanonicalName(); private Context context; private DownloadManager downloadManager = null; private static DownloadManagerUtil instance = null; private DownloadManagerCallback callback = null; class DownloadItem { long id; String fileName; String url; public DownloadItem (long id, String fileName, String url) { this.id = id; this.fileName = fileName; this.url = url; } } public static DownloadManagerUtil getInstance(Context context) { if (instance == null) { synchronized (DownloadManagerUtil.class) { if (instance == null) { instance = new DownloadManagerUtil(context); } } } return instance; } private DownloadManagerUtil(Context context) { this.context = context; if (downloadManager == null) { downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); } RegisterDownload(); } public void RegisterDownload() { IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); context.registerReceiver(mReceiver, filter); } public void SetDMCallback(final DownloadManagerCallback cb) { callback = cb; } private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); DownloadManager.Query query = new DownloadManager.Query(); Cursor cursor = downloadManager.query(query); while (cursor != null && cursor.moveToFirst()) { int cid = cursor.getInt( cursor.getColumnIndex(DownloadManager.COLUMN_ID) ); int status = cursor.getInt( cursor.getColumnIndex(DownloadManager.COLUMN_STATUS) ); if (id == cid && status == DownloadManager.STATUS_SUCCESSFUL) { String title = cursor.getString( cursor.getColumnIndex(DownloadManager.COLUMN_TITLE) ); String fileUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)); Log.d(TAG, "下载完毕 " + fileUri); if (callback != null) { callback.DownloadComplete(title, Uri.parse(fileUri).getPath()); } break; } } } } }; public void DownloadFile(String url, String saveFileName, String title, String desc) { Log.d(TAG, "下载文件 " + url); // 跳过已经在下载的 if (CheckAndClear(title) == 1) { Log.d(TAG, "跳过已经在下载的 " + title); return; } DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), saveFileName); request.setDestinationUri(Uri.fromFile(file)); request.setTitle(title); request.setDescription(desc); if (downloadManager != null) { long idx = downloadManager.enqueue(request); Log.d(TAG, "新建下载任务 id = " + idx); } } public int CheckAndClear(String title) { DownloadManager.Query query = new DownloadManager.Query(); Cursor cursor = downloadManager.query(query); while (cursor != null && cursor.moveToNext()) { String theTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)); if (theTitle.equals(title)) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); long id = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)); Log.d(TAG, "当前该文件下载状态 " + status); if (status == DownloadManager.STATUS_FAILED || status == DownloadManager.STATUS_SUCCESSFUL) { downloadManager.remove(id); return 0; } else if (status != DownloadManager.STATUS_RUNNING) { // 如果是多次处于暂停\挂起状态,还是删除 downloadManager.remove(id); return 0; } return 1; } } return 0; } public int CheckDownloadStatus(int id) { DownloadManager.Query query = new DownloadManager.Query(); Cursor cursor = downloadManager.query(query); while (cursor != null && cursor.moveToNext()) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); if (id == cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_ID))) { return status; } } return -1; } }