-4

How can I show downloading status bar similar to android default browser:

1 Answers1

1

You really should search before post a question here my friend.

There is a component in Android API that could help you called DownloadManager.


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 “de.vogella.android.downloadmanager” 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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="Start Download" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="onClick"></Button>
    <Button android:text="View Downloads" android:id="@+id/button2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="showDownload"></Button>
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
</LinearLayout>

Change the code of your activity to the following. package de.vogella.android.downloadmanager;

import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            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();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {

                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            view.setImageURI(Uri.parse(uriString));
                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
        enqueue = dm.enqueue(request);

    }

    public void showDownload(View view) {
        Intent i = new Intent();
        i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
        startActivity(i);
    }
}

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.

Source: http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

Idemax
  • 2,712
  • 6
  • 33
  • 66