Sunday, 24 March 2013

Example On Download Manager

Android has introduced new class to work on download mechanism. "Download Manager" class has been added at API level 9.  

Today,  i will tell you how to use download manager class functionality in our android app. traditionally we used  Http with Inputstream/outputstream for downloading any data in android app.

for using Download Manager in app, we have to learn its subclasses & its respective mehods.....

Download Manager having two classes, 
1) DownloadManager.Query 
        It helps to filter dowload manaer queries.which means it fires download related queries.& gives  results on it,like how much download has completed, downloaded file exist ... n many more....

2) DownloadManger.Request 
               It helps to create new Download Request.with these class we can specify what to downlod,where to put downloaded file on ... n many more 

Lets create an app.....................

1) create an project with name of DownloadDataActivity(Here i used,these name)

2)build target above -android api 14 (above 9)

lets code, together

after creating project.. lets first modified our layout ....
as we know we are going to download data (here we take an image).
so we need one button on our layout , when we hit on it ,.... certain download related event get fired & download get started.
so , inside on 

******************* main.xml **************************

<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="Start Downloading my File" />

        android:text="Display all Downloads" />


we used one button over here,for starting downloading data.

& inside Android Manifest.xml
we need to declare these uses-permission 

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

we simply want to create event for start dowload button which can initiate download with respect to querry. 
so code will be looks like .... 


first we need to assign few varible which we variable that we can use in our code further,

DownloadManager downloadManager;
Reqest request;
long downloadReference;

then in ,oncreate code will be look like,

        public void onCreate(Bundle savedInstanceState) {

                downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                 Button startDownload = (Button)                            findViewById(;

startDownload .setOnclicklistener(new View.OnClickListener() {
                public void onClick(View v) {
then, inside click event , 
we have to assign 
here we have to fire querry for downloding queery by using following 

request = new Request(Uri.parse(""));

downloadReference = downloadManager.enqueue(request);

Where, downloadReference is long variable ,which  helps to download with help of function of enqueue. 

No comments:

Post a Comment