首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在活动中显示下载管理器进度

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

Stack Overflow用户
提问于 2013-02-10 22:07:25
回答 3查看 24.5K关注 0票数 19

我在我的活动中使用了Download Manager类来执行下载;它工作得很好,我的下一个任务是在我的活动中显示相同的进度百分比。我不知道该怎么做。

到目前为止我的代码

代码语言:javascript
复制
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);
}
}

有没有给出进度下载百分比的方法?

EN

回答 3

Stack Overflow用户

发布于 2014-07-23 02:50:49

如果您正在寻找一种合适的方法来确定何时查询DownloadManager以获取进度更新,请考虑为uri content://downloads/my_downloads注册一个ContentObserver

示例:

代码语言:javascript
复制
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" );
   }

当接收到长时间运行的下载的每个块时,这将产生以下输出

代码语言:javascript
复制
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)

票数 26
EN

Stack Overflow用户

发布于 2013-06-07 03:58:31

您可以使用query方法查询到目前为止已下载的字节数和需要下载的总字节数,方法与在示例代码中查询状态的方式大致相同。一旦有了这些值,就很容易以百分比的形式计算进度。

当收到新数据时,似乎没有任何方法通知您,因此,您可以定期轮询下载管理器,以确定您想要监控的任何下载的当前状态。

代码语言:javascript
复制
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头部)。如果您需要支持这种类型的服务器,您可能应该向用户提供某种类型的指示,表明下载正在进行,而不仅仅是一个停留在零的进度条。

票数 23
EN

Stack Overflow用户

发布于 2017-02-17 18:41:34

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

代码语言:javascript
复制
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();
    }

进入文件队列的函数是:

代码语言:javascript
复制
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);
    }

基本上,我会分别收到下载已完成的每个文件的通知,然后将它们添加到一个集合中,该集合基本上就是帮助我确定所有下载是否都已完成的集合。我根据文件的数量和每个文件的完成程度来跟踪进度。我希望这能帮到你。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14798569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档