在活动内显示下载管理器进度

20

我在我的活动中使用下载管理器类来执行下载操作;它运行良好,我的下一个任务是在我的活动中显示同样的进度百分比。我不确定该如何实现。

到目前为止我的代码:

public class DownloadSampleBook extends Activity{

private long enqueue;
private DownloadManager dm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_download);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                Query query = new Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {

                       view.setImageURI(Uri.parse(uriString));
                    }
                }
            }
        }
    };

    registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

public void onClick(View view) {
    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(
            Uri.parse("http://abc.com/a.png"));
    enqueue = dm.enqueue(request);

}

public void showDownload(View view) {
    Intent i = new Intent();
    i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
    startActivity(i);
}
}

有没有一种方法可以给出下载进度的百分比?


1
想知道这个问题的答案。目前没有明确的解决方案。 - Bagusflyer
已经回答了这个问题https://dev59.com/u1kJtIcB2Jgan1znExEn#73546957。 - Quratulain Naqvi
3个回答

26
如果你想找到一个不错的方法来确定何时查询DownloadManager以获取进度更新,请考虑为uri content://downloads/my_downloads注册ContentObserver
示例:
DownloadManager manager = (DownloadManager) getSystemService( Context.DOWNLOAD_SERVICE );
manager.enqueue( myRequest );

Uri myDownloads = Uri.parse( "content://downloads/my_downloads" );
getContentResolver().registerContentObserver( myDownloads, true, new DownloadObserver() );

...

public static class DownloadObserver extends ContentObserver {
   @Override
   public void onChange( boolean selfChange, Uri uri ) {
      Log.d( "DownloadObserver", "Download " + uri + " updated" );
   }

在接收到长时间运行的下载的每个块时,将输出以下内容:

D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated

这里的“437”是您下载的ID。

请注意,这遵循在类android.provider.Downloads中定义的内容URI,该类似乎在框架中隐藏,并且可能在所有设备上无法一致工作。(https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/Downloads.java#89)


25
您可以使用“query”方法查询已下载的字节数和需要下载的总字节数,方式与您在示例代码中查询状态的方式非常相似。一旦获得这些值,计算进度百分比就相对容易了。
似乎没有任何通知您新数据已接收的方式,因此需要定期轮询下载管理器以确定您想要监视的任何下载的当前状态。
Query query = new Query();
query.setfilterById(downloadId);

Cursor c = dm.query(query);
if (c.moveToFirst()) {
  int sizeIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
  int downloadedIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
  long size = c.getInt(sizeIndex);
  long downloaded = c.getInt(downloadedIndex);
  double progress = 0.0;
  if (size != -1) progress = downloaded*100.0/size;  
  // At this point you have the progress as a percentage.
}

请注意,总大小最初将为-1,只有在下载开始后才会填充。因此,在上面的示例代码中,如果尚未设置大小,则我已检查了-1并将进度设置为0。

但是,在某些情况下,您可能会发现总大小永远不会返回(例如,在HTTP分块传输中,将没有Content-Length标头可用于确定大小)。如果您需要支持这种类型的服务器,则应该向用户提供某种指示,表明下载正在进行,而不仅仅是停留在零的进度条。


应该检查大小是否为零值吗?类似于downloaded*100.0/(size == 0 ? 1 : size)这样的内容。 - Fabiano

6

我有一个需求,需要跟踪多个文件的下载情况。经过很多思考和实验,我想出了以下代码:

private void startDownloadThread(final List<DownloadFile> list) {

        // Initializing the broadcast receiver ...
        mBroadCastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mFinishedFilesFromNotif.add(intent.getExtras()
                        .getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
            }
        };

        IntentFilter intentFilter = new IntentFilter(
                "android.intent.action.DOWNLOAD_COMPLETE");
        DownloadProgressUIFragment.this.getActivity().registerReceiver(mBroadCastReceiver,
                intentFilter);

        // initializing the download manager instance ....
        mDownloadManager = (DownloadManager) getActivity()
                .getSystemService(Context.DOWNLOAD_SERVICE);

        // adding files to the download manager list ...
        for(DownloadFile f: list) {
            mDownloadIds.add(FileUtils.addFileForDownloadInBkg(getApplicationContext(),
                    f.getUrl(),
                    f.getPath()));
        }

        // starting the thread to track the progress of the download ..
        mProgressThread = new Thread(new Runnable() {
            @Override
            public void run() {

                // Preparing the query for the download manager ...
                DownloadManager.Query q = new DownloadManager.Query();
                long[] ids = new long[mDownloadIds.size()];
                final List<Long> idsArrList= new ArrayList<>();
                int i = 0;
                for (Long id: mDownloadIds) {
                    ids[i++] = id;
                    idsArrList.add(id);
                }
                q.setFilterById(ids);

                // getting the total size of the data ...
                Cursor c;

                while(true) {

                    // check if the downloads are already completed ...
                    // Here I have created a set of download ids from download manager to keep
                    // track of all the files that are dowloaded, which I populate by creating
                    //
                    if(mFinishedFilesFromNotif.containsAll(idsArrList)) {
                        isDownloadSuccess = true;

                        // TODO - Take appropriate action. Download is finished successfully
                        return;
                    }

                    // start iterating and noting progress ..
                    c = mDownloadManager.query(q);
                    if(c != null) {
                        int filesDownloaded = 0;
                        float fileFracs = 0f; // this stores the fraction of all the files in
                        // download
                        final int columnTotalSize = c.getColumnIndex
                                (DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
                        final int columnStatus = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        //final int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID);
                        final int columnDwnldSoFar =
                                c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);

                        while (c.moveToNext()) {
                            // checking the progress ..
                            if(c.getInt(columnStatus) == DownloadManager.STATUS_SUCCESSFUL) {
                                filesDownloaded++;
                            }
                            // If the file is partially downloaded, take its fraction ..
                            else if(c.getInt(columnTotalSize) > 0) {
                                fileFracs += ((c.getInt(columnDwnldSoFar) * 1.0f) /
                                        c.getInt(columnTotalSize));
                            } else if(c.getInt(columnStatus) == DownloadManager.STATUS_FAILED) {
                                // TODO - Take appropriate action. Error in downloading one of the
                                // files.
                                return;
                            }
                        }

                        c.close();

                        // calculate the progress to show ...
                        float progress = (filesDownloaded + fileFracs)/ids.length;

                        // setting the progress text and bar...
                        final int percentage = Math.round(progress * 100.0f);
                        final String txt = "Loading ... " + percentage + "%";

                        // Show the progress appropriately ...
                    }
                }
            }
        });

        mProgressThread.start();
    }

将文件加入队列的函数如下:

public static long addFileForDownloadInBkg(Context context, String url, String savePath) {
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
        request.setDestinationUri(Uri.fromFile(new File(savePath)));

        final DownloadManager m = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        return m.enqueue(request);
    }

基本上,每次下载完成一个文件我都会收到一条通知,然后将它们添加到一个集合中。这个集合基本上是帮助我判断所有下载是否已经完成的依据。我根据文件数量和每个文件完成程度的比例来跟踪进度。希望这可以帮助你理解。

2
嘿,Swapnil,你能否提供整个源代码吗? - vishva kapadia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接