Download Manager in Android with Example

 


                  The Android Download Manager was introduced in Android 2.3 as a service to optimize the handling of long-running downloads. The Download Manger handles the HTTP connection and monitors connectivity changes. Its a good practice to use Download Manager in most situations, particularly where a download is likely to continue in the background between user sessions.
        To access the Download Manager request the DOWNLOAD_SERVICE using the getSystemService().
String servicestring = Context.DOWNLOAD_SERVICE;
DownloadManager downloadanager;     
downloadanager=(DownloadManager)getSystemService(servicestring);

                     To request a download, create DownloadManager.Request specifying the URI of the file to Download and passing it to the DownloadManager enqueue method. 

Activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="Download" />

</RelativeLayout>

MainActivity.java

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
 private Button button;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View view) {
    String servicestring = Context.DOWNLOAD_SERVICE;
    DownloadManager downloadmanager;
    downloadmanager = (DownloadManager) getSystemService(servicestring);
    Uri uri = Uri
      .parse("https://sites.google.com/site/compiletimeerrorcom/android-programming/oct-2013/DownloadManagerAndroid1.zip");
    DownloadManager.Request request = new Request(uri);
    Long reference = downloadmanager.enqueue(request);
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}


Notification for Download

Download SourceCode

3 comments:
Write comments
  1. but where the file located after download is finished ?!

    ReplyDelete
  2. File will be located in the users phones Download folder.

    ReplyDelete

Popular Posts

Find us on Facebook