最好的方式来下载一个文件在Android中? [英] best way to download a file in android with?

查看:120
本文介绍了最好的方式来下载一个文件在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下载活动,下载我的应用程序的一些zip文件。
我希望我的活动有这个功能:

I have a download activity which downloads some zip files for my application. I want my activity have this features :


  • 能够暂停/恢复下载

  • 通过进度条显示我的活动UI中的​​下载进度(不是使用
    dialogProgress)

  • 显示下载进度的通知,当用户点击
    通知打开时我的活动,即使应用程序关闭,我的UI不断更新下载

我搜索了很多,看到很多方法可以用于下载文件(如使用AsyncTask / Downloadmanager / Groundy ...),但我不知道其中哪一个具有我想要的功能

I have searched a lot and saw lots of methods that can be used to download files (like using AsyncTask/Downloadmanager/Groundy ... ) but I don't know which one of them has the features I want

你认为最好的方法来实现这个功能?

我不想要你的完整代码,只是一些提示和方法或参考,以帮助我实现这些功能,并找到最好的方法。

I don't want the full code from you, just some tips and methods or references to help me implement each of these features and find the best way.

感谢您的时间。

推荐答案

从根本上说,您可以使用AsyncTask,通过它可以实现上述功能,如果下载与活动严格相关。但是,一旦用户取消或退出活动,则需要正确取消AsyncTask。同时它也为进步的事情提供了基本的机制。

Fundamentally you can use AsyncTask through which you would be able to achieve the above, if the download is strictly tied to the activity. However AsyncTask needs to be properly cancelled once user cancel or back out from activity. Also it provide basic mechanism to do the progress thing.

例如,想象一个简单的异步任务(不是最好的实现),但是像下面这样的东西

For example, imagine a simple async task (not the best implementation) but something like below

class SearchAsyncTask extends AsyncTask<String, Void, Void> {

        SearchHttpClient searchHttpClient = null;

        public SearchAsyncTask(SearchHttpClient client) {
            searchHttpClient = client;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // You can update the progress on dialog here
            super.onProgressUpdate(values);
        }

        @Override
        protected void onCancelled() {
            // Cancel the http downloading here
            super.onCancelled();
        }

        protected Void doInBackground(String... params) {

            try {

                // Perform http operation here  
                // publish progress using method publishProgress(values)
                return null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute() {
                 // If not cancelled then do UI work

        }
    }

现在您的活动的onDestroy方法

Now in your activity's onDestroy method

@Override
protected void onDestroy() {
    Log.d(TAG, "ResultListActivity onDestory called");
    if (mSearchTask != null && mSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
    // This would not cancel downloading from httpClient 
    //  we have do handle that manually in onCancelled event inside AsyncTask
        mSearchTask.cancel(true); 
        mSearchTask = null;
    }
    super.onDestroy();
}

但是,如果允许用户下载文件,甚至独立于活动(就像继续)即使用户离开了Activity,我也建议使用一个可以在后台执行的服务。

However if you allow user to Download file even independent of activity (like it continues downloading even if user got away from Activity), I would suggest to use a Service which can do the stuff in background.

更新:只是注意到有一个类似的答案Stackoverflow更详细地解释了我的想法使用Android下载文件,并在ProgressDialog中显示进度

UPDATE: Just noticed there is a similar answer on Stackoverflow which explains my thoughts in further detail Download a file with Android, and showing the progress in a ProgressDialog

这篇关于最好的方式来下载一个文件在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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