繁体   English   中英

同时下载文件-处理文件的最佳方法

[英]simultaneous download of files - best way to handle it

我正在编写针对API级别9或更高级别的应用程序。 因此,我决定使用SDK提供的DownloadManager类。 我的问题是2部分-1。当我下载一个文件时,如何显示下载进度。 我看到我可以从查询下载管理器实例中获得COLUMN_TOTAL_SIZE_BYTES和COLUMN_BYTES_DOWNLOADED_SO_FAR。 但是我不确定是否必须将查询放入线程并实现循环,以便可以定期轮询以更新进度栏。 我猜我不确定如何定期查询-它会进入主线程还是作为可运行的实现-我不清楚这种机制。 2.如果我必须支持多个文件下载,那么是否必须在自己的线程中启动每个文件下载? 谢谢。

如果您使用的是2.3及更高版本,则不需要,它会自动在状态栏中显示它。 这样做,来自通用软件的代码,

    private static final int DOWNLOAD_SUCCESSFUL = 100;
private static final int DOWNLOAD_FAILED = 99;
    private DownloadManager mgr=null;

在创建时,

`mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);    

        registerReceiver(onComplete,
                 new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

`

然后在文件下载的点击事件中,

lastDownload = mgr.enqueue(new DownloadManager.Request(uri) .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle("Test File") .setDescription("Download zipped file.") .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file_name));

然后是广播接收器,用于您的下载竞争时,

BroadcastReceiver onComplete=new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {

            findViewById(R.id.start).setEnabled(true);  

            File unzipFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),file_name);            

            //check for new file download
            //extract if it's a new download

            if (unzipFile.exists()) {
                new UnzipFile().execute();              
            } else {
                Toast.makeText(Main.this, "Download not found!", Toast.LENGTH_LONG).show();
            }           
        }
    };`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM