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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
> 

    <Button
        android:id="@+id/startDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Downloading my File" />

    <Button
        android:id="@+id/displayDownload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Display all Downloads" />

   
</LinearLayout>

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" />



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


**********************DownloadDataActivity.java*************

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,

@Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
       
                setContentView(R.layout.activity_download_data);
                 Button startDownload = (Button)                            findViewById(R.id.startDownload);

startDownload .setOnclicklistener(new View.OnClickListener() {
               
                @Override
                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("http://mirror-us-ga1.gallery.hd.org/_exhibits/natural-science/_more2002/_more04/pelicans-enjoying-water-sprinkler-on-lawn-2-MB.jpg"));

downloadReference = downloadManager.enqueue(request);


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








No comments:

Post a Comment