Click here to Skip to main content
13,146,014 members (43,143 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 1 Dec 2011

Downloading multiple files using AsyncTask in Android

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
This article teaches you how to download multiple files using progressBar, Notification, and AsyncTask.


This article shows how to download multiple files using progressBar, Notification, and AsyncTask.

Using the code

  1. The first thing to do is check the connection using the isInternetConnectionActive(context) method. If this method return true, then create a notification for showing a message:
  2. private static final int HELLO_ID = 1;
    public void onCreate(Bundle savedInstanceState) {
        if(isInternetConnectionActive(getApplicationContext())) {
        else {
    private boolean isInternetConnectionActive(Context context) {
        	NetworkInfo networkInfo = ((ConnectivityManager) context
      if(networkInfo == null || !networkInfo.isConnected()) {
         return false;
         return true;
    private void showNotification(int status){
        	String ns = Context.NOTIFICATION_SERVICE;
       NotificationManager mNotificationManager = 
           (NotificationManager) getSystemService(ns);
       int icon = android.R.drawable.ic_media_play;
       CharSequence ticketText = "Connection";
       long when = System.currentTimeMillis();
       Notification notification = new Notification(icon, ticketText, when);
       Context context = getApplicationContext();
       CharSequence contentTitle = "Internet Connection";
       CharSequence contentText = "";
       if(status == 1){
         	contentText = "Internet connected, Are you Update your Application?";
        	contentText = "Internet disconnected!";
       Intent notificationIntent = new Intent(this, NotifyPage.class);
       PendingIntent contentIntent = 
         PendingIntent.getActivity(this, 0, notificationIntent, 0);
       notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
       mNotificationManager.notify(HELLO_ID, notification);
  3. In the showNotification() method, we define an Intent, declare its second parameter as NotifyPage.class. When the user selects a notification, call the activity NotifyPage. For this activity, design an XML file that includes two buttons: a button that creates another notification for showing a progressBar() and a button to cancel download operations and come back home.
  4. public void clearNotification(View v) {
    public void UpdateApplication(View v){
    	new DownloadTask(this).execute("" + 
  5. In the DownloadTask class, you can send several files but in this example, we just send a file address. In fact, with execute(url), you call doInBackground() that runs downloading operations in the background. You can download notification calling using four methods in the DownloadTask class:
  6. public  DownloadTask(Context context){
       this.context = context;
       //Create the notification object from NotificationHelper class
       mNotificationHelper = new NotificationHelper(context);
    protected void onPreExecute(){
       //Create the notification in the statusbar
    protected void onProgressUpdate(Integer... progress) {
       //This method runs on the UI thread, it receives progress updates
       //from the background thread and publishes them to the status bar
    protected void onPostExecute(Void result)    {
       //The task is complete, tell the status bar about it

Remember to define permissions related to connection in AndroidManifest.xml: android.permission.INTERNET - android.permission.ACCESS_NETWORK_STATE.

Points of Interest

This article hopefully taught you how to create a status bar notification and use a AsyncTask class for downloading or uploading files.


This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0


About the Author

Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
davidlow7015-Jan-12 13:05
memberdavidlow7015-Jan-12 13:05 
QuestionSomething's amiss Pin
DavidCrow8-Dec-11 9:36
memberDavidCrow8-Dec-11 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by omid.nazifi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid