Android DownloadManager Example

The Android DownloadManager introduced in Android 2.3. (API 9) is a system service which allows to handle long-running HTTP downloads in the background and notify the triggering application via a broadcast receiver once the download is finished.

Here is a little example for using the DownloadManager. The project will be called “” with the activity “DownloadManagerActivity” based on Android API9 or higher.

Change “main.xml” to the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
	android:orientation="vertical" android:layout_width="fill_parent"
	<Button android:text="Start Download" android:id="@+id/button1"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
	<Button android:text="View Downloads" android:id="@+id/button2"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
	<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
		android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

Change the code of your activity to the following.


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class DownloadManagerActivity extends Activity {
	private long enqueue;
	private DownloadManager dm;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {

		BroadcastReceiver receiver = new BroadcastReceiver() {
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
					long downloadId = intent.getLongExtra(
							DownloadManager.EXTRA_DOWNLOAD_ID, 0);
					Query query = new Query();
					Cursor c = dm.query(query);
					if (c.moveToFirst()) {
						int columnIndex = c
						if (DownloadManager.STATUS_SUCCESSFUL == c
								.getInt(columnIndex)) {

							ImageView view = (ImageView) findViewById(;
							String uriString = c

		registerReceiver(receiver, new IntentFilter(

	public void onClick(View view) {
		dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
		Request request = new Request(
		enqueue = dm.enqueue(request);


	public void showDownload(View view) {
		Intent i = new Intent();

Also add the permission to go to the internet to your app.

If you implemented this example you have an Android application which can download my picture (sorry for this ;-)) and allow you to switch to the download manager to see the finished downloads.

Hope this helps.

You find me also on Twitter.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android. Bookmark the permalink.

6 Responses to Android DownloadManager Example

Comments are closed.