0

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

  • capable of pause/resume download
  • shows download progress in my activity's UI by a progress bar ( not with dialogProgress)
  • shows notification with download progress and when user clicks on the notification it opens my activity even when the app is closed and my UI keeps updated during download

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

what do you think is the best way to achieve this features all together ??

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.

Thanks for your time.

Saeed.re
  • 740
  • 9
  • 19

1 Answers1

0

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

        }
    }

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();
}

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.

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

Community
  • 1
  • 1
Adil
  • 3,248
  • 1
  • 22
  • 27
  • zip files size is a bit high (about 50mb) so I guess the user don't want to stay at activity and watch the download process or maybe they want to pause the process for a while, So AsyncTask may not be a good approach for me. I read that link before and I'm not sure which one of the the methods mentioned there can do the things I want. ( Download manager is the last way I think about because I want my own download process, maybe with a custom notification) – Saeed.re Jun 29 '14 at 23:45
  • Then in this scenario you can create a service and let it do the stuff in background. – Adil Jun 29 '14 at 23:54
  • yes I think service is a good method to achieve what I want, By the way do you know how I can pause and resume the download process in service ?, I read somewhere that by storing the downloaded size in cache it can be done but I've never worked with cache before :( .. – Saeed.re Jun 30 '14 at 13:47