Android下载管理器进度

Android下载管理器进度,android,download-manager,Android,Download Manager,我正在开发一个应用程序,用户可以下载不同的内容包。对于下载过程,我使用DownloadManager类。到目前为止效果不错 我的问题是,如何获取由DownloadManager启动的正在运行的下载的当前进度。我知道有buildin下载通知等等。但对我来说,有必要获得正在运行的下载进度,以便我可以使用它在我的应用程序中的自定义进度栏中显示进度。到目前为止,我无法检索进度 这是可能的还是我只是瞎了眼,找不到解决办法 希望有人能帮助我……我也在寻找更好的方法,但到目前为止,我计划每1秒左右进行一次进度

我正在开发一个应用程序,用户可以下载不同的内容包。对于下载过程,我使用DownloadManager类。到目前为止效果不错

我的问题是,如何获取由DownloadManager启动的正在运行的下载的当前进度。我知道有buildin下载通知等等。但对我来说,有必要获得正在运行的下载进度,以便我可以使用它在我的应用程序中的自定义进度栏中显示进度。到目前为止,我无法检索进度

这是可能的还是我只是瞎了眼,找不到解决办法


希望有人能帮助我……

我也在寻找更好的方法,但到目前为止,我计划每1秒左右进行一次进度调查

DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = mgr.enqueue(request);

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(id);
Cursor cursor = mgr.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
cursor.close();
编辑:

A可以帮上忙。这是一个框架,我把它放在一起,帮助跟踪我们的应用程序下载了哪些文件。在活动或服务的
onStart
中启动它,并在
onStop
中停止它。再加上在启动过程中手动同步事物的状态,这可以让您对正在发生的事情有一个相当完整的了解

特别是对于进度,观察打开/关闭写入事件可以帮助您决定何时开始/停止轮询DownloadManager以获取更新

public class DownloadsObserver extends FileObserver {

    public static final String LOG_TAG = DownloadsObserver.class.getSimpleName();

    private static final int flags =
            FileObserver.CLOSE_WRITE
            | FileObserver.OPEN
            | FileObserver.MODIFY
            | FileObserver.DELETE
            | FileObserver.MOVED_FROM;
    // Received three of these after the delete event while deleting a video through a separate file manager app:
    // 01-16 15:52:27.627: D/APP(4316): DownloadsObserver: onEvent(1073741856, null)

    public DownloadsObserver(String path) {
        super(path, flags);
    }

    @Override
    public void onEvent(int event, String path) {
        Log.d(LOG_TAG, "onEvent(" + event + ", " + path + ")");

        if (path == null) {
            return;
        }

        switch (event) {
        case FileObserver.CLOSE_WRITE:
            // Download complete, or paused when wifi is disconnected. Possibly reported more than once in a row.
            // Useful for noticing when a download has been paused. For completions, register a receiver for 
            // DownloadManager.ACTION_DOWNLOAD_COMPLETE.
            break;
        case FileObserver.OPEN:
            // Called for both read and write modes.
            // Useful for noticing a download has been started or resumed.
            break;
        case FileObserver.DELETE:
        case FileObserver.MOVED_FROM:
            // These might come in handy for obvious reasons.
            break;
        case FileObserver.MODIFY:
            // Called very frequently while a download is ongoing (~1 per ms).
            // This could be used to trigger a progress update, but that should probably be done less often than this.
            break;
        }
    }
}
用法如下:

public class MyActivity extends Activity {

    private FileObserver fileObserver = new DownloadsObserver(
            getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());

    @Override
    protected void onStart() {
        super.onStart();
        fileObserver.startWatching();
        syncUpDatabaseWithFileSystem();
    }

    @Override
    protected void onStop() {
        fileObserver.stopWatching();
        super.onStop();
    }
}

原来,棉花糖上的
FileObserver
实现有一个bug。因此,
FileObserver
不会报告下载管理器下载的文件的任何修改。(较旧的Android版本没有这个问题——它在KitKat上运行良好。)

对我来说,以下代码(基于)运行良好。我每秒钟进行一次民意调查——我试着将时间间隔减半,但没有任何明显的效果

private static final int PROGRESS_DELAY = 1000;
Handler handler = new Handler();
private boolean isProgressCheckerRunning = false;

// when the first download starts
startProgressChecker();

// when the last download finishes or the Activity is destroyed
stopProgressChecker();

/**
 * Checks download progress.
 */
private void checkProgress() {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterByStatus(~(DownloadManager.STATUS_FAILED | DownloadManager.STATUS_SUCCESSFUL));
    Cursor cursor = downloadManager.query(query);
    if (!cursor.moveToFirst()) {
        cursor.close();
        return;
    }
    do {
        long reference = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
        long progress = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
        // do whatever you need with the progress
    } while (cursor.moveToNext());
    cursor.close();
}

/**
 * Starts watching download progress.
 * 
 * This method is safe to call multiple times. Starting an already running progress checker is a no-op.
 */
private void startProgressChecker() {
    if (!isProgressCheckerRunning) {
        progressChecker.run();
    isProgressCheckerRunning = true;
    }
}

/**
 * Stops watching download progress.
 */
private void stopProgressChecker() {
    handler.removeCallbacks(progressChecker);
    isProgressCheckerRunning = false;
}

/**
 * Checks download progress and updates status, then re-schedules itself.
 */
private Runnable progressChecker = new Runnable() {
    @Override
    public void run() {
        try {
            checkProgress();
            // manager reference not found. Commenting the code for compilation
            //manager.refresh();
        } finally {
            handler.postDelayed(progressChecker, PROGRESS_DELAY);
        }
    }
};

在修改的情况下,我们如何获得下载的价值?如何知道下载部分的百分比?对我来说非常有用!但是,请记住,
FileObserver#onEvent()
在单独的线程上运行。对于任何UI操作(例如更新进度条),都需要执行类似于
newhandler(Looper.getMainLooper()).post(newrunnable(){…})的操作并将代码放入
Runnable
run()
方法中。修改:对KitKat很有效,但对棉花糖无效。据报道,这似乎是棉花糖中的一个bug……在安卓7上确实有效。