【问题标题】:How to access the percent of Android download manager?如何访问 Android 下载管理器的百分比?
【发布时间】:2016-06-08 20:49:59
【问题描述】:

我在我的应用中使用 Android 下载管理器。如何访问下载进度(如通知中的下载百分比)以在我的活动内的进度栏中显示该百分比?

这是我下载文件的方法:

////////////////////////////////////////////////////////////
//================== btn download set on click listner  ===========
////////////////////////////////////////////////////////////

holder.btnDownload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(questionha.get(position).getQuestionDownLink());
        if (!Uri.EMPTY.equals(downloadUri)) {

            Request request = new Request(downloadUri);
            request.setTitle(questionha.get(position).getQuestionTitle());
            request.setDescription("Downloading ....");
            request.setDestinationInExternalFilesDir(v.getContext(), "downloaded", questionha.get(position).getQuestionDownFileName());
            enqueue = dm.enqueue(request);
        }else {
            Log.i("Download manager","Download manager url is null !");
        }

    }
});

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    在 Android 下载管理器文档中搜索后,我找到了这个解决方案:

    String urlDownload = "something";
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));
    
    request.setDescription("This is Desc");
    request.setTitle("download picsArt");
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");
    
    final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    
    final long downloadId = manager.enqueue(request);
    
    final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
    
    new Thread(new Runnable() {
    
        @Override
        public void run() {
    
            boolean downloading = true;
    
            while (downloading) {
    
                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(downloadId);
    
                Cursor cursor = manager.query(q);
                cursor.moveToFirst();
                int bytes_downloaded = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
    
                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                }
    
                final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
    
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
    
                        mProgressBar.setProgress((int) dl_progress);
    
                    }
                });
                cursor.close();
            }
    
        }
    }).start();
    

    【讨论】:

    • 但是这段代码对我有用并更新了进度条,你怎么看?
    • 我认为您可能没有特别充分的理由通过不断检查该值来浪费电池。我不知道这是否是您可以收听的事件或其他方式,但至少您希望在查询数据库后稍等片刻,直到您再次执行此操作。
    • 您也许可以使用 ContentObserver 来监视更改而不是循环:registerContentObserver( Uri.parse( "content://downloads/my_downloads" ), true, YOUR_OBSERVER );
    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2011-11-29
    • 2014-02-28
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多