Click here to Skip to main content
15,896,269 members
Articles / Mobile Apps / Android

Downloading multiple files using AsyncTask in Android

Rate me:
Please Sign up or sign in to vote.
4.22/5 (8 votes)
1 Dec 2011Eclipse 62K   3.9K   24  
This article teaches you how to download multiple files using progressBar, Notification, and AsyncTask.
package com.crcis.downloader;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

public class DownloaderActivity extends Activity {
    /** Called when the activity is first created. */
    private static final int HELLO_ID = 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        if(isInternetConnectionActive(getApplicationContext())) {
        	showNotification(1);
       	}
       	//else {
       		//showNotification(0);
       	//}
    }
     
    private boolean isInternetConnectionActive(Context context) {
    	NetworkInfo networkInfo = ((ConnectivityManager) context
    	    .getSystemService(Context.CONNECTIVITY_SERVICE))
    	    .getActiveNetworkInfo();

    	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?";
        }
        else{
        	contentText = "Internet disconnected!";
        }
        
        Intent notificationIntent = new Intent(this, NotifyPage.class);
        notificationIntent.addFlags(Notification.FLAG_ONGOING_EVENT);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(HELLO_ID, notification);
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions