Android的下载管理进展 [英] Android DownloadManager Progress

查看:129
本文介绍了Android的下载管理进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,用户可以下载不同的内容包。有关下载过程我使用的下载管理器类。那是工作的罚款为止。

i'm developing an app where users are able to download different content packages. For the download process i am using the DownloadManager class. Thats working fine so far.

我的问题是怎样才能在运行中下载这是开始与下载管理目前的进展。我知道,还有就是buildin下载通知等。但对我来说,这是必要的,我得到的运行下载的进度,所以我可以用它来展现在我的应用程序自定义进度的进度。到目前为止,我无法取得进展。

My Question is how can i get the current progress of a running Download which was started with the DownloadManager. I know that there is the buildin Download Notification and so on. But for me it is necessary that i get the progress of the running download so i can use it to show the progress in a custom progressbar in my app. So far i was not able to retrieve the progress.

它甚至有可能还是我只是盲目的,不能找到解决办法。

Is it even possible or am i just blind and can't find the solution.

希望有人能帮助我...

Hopefully someone can help me...

推荐答案

我要寻找这样做也有一个更好的办法,但到目前为止,我打算只轮询进度每1秒左右。

I am looking for a better way of doing this also, but so far I am planning to just poll for progress every 1sec or so.

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 <一个href="http://developer.android.com/reference/android/os/FileObserver.html"><$c$c>FileObserver可以帮助与此有关。这是一个我已经把以帮助跟踪哪些文件了我们的应用程序已经下载的骨架。开始它的活动或服务的 ONSTART 并停止它在的onStop 。在 ONSTART 结合事物的状态的手动同步,这可以让你发生了什么事情在pretty的全貌。

A FileObserver can help with this. This is the skeleton of one I have put together to help keep track of which files our app has downloaded. Start it in an activity or service's onStart and stop it in onStop. Combined with a manual synchronization of the state of things during onStart, this can give you a pretty complete picture of what's going on.

有关具体的进展,看的开/ CLOSE_WRITE事件可以帮助你决定​​什么时候开始/停止投票的下载管理器更新。

For progress in particular, watching for the OPEN/CLOSE_WRITE events can help you decide when to start/stop polling the DownloadManager for updates.

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;
        }
    }
}

用法是这样的:

Usage would be something like this:

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();
    }
}

这篇关于Android的下载管理进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆