Show progress bar while downloading using download manager
Asked Answered
M

2

6

I am using download manager api for downloading large files, and i achieved it too. But the thing is, the notification of downloading progress is showing in status bar. But i want to show the notification inside the activity using Progress bar. So do anyone have done it, if so plz help me.

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);

}
Morbilli answered 20/11, 2013 at 9:49 Comment(4)
you can active progress bar in onClick. and dismiss him in onRecieve.Aliciaalick
How can i show the progress bar progressing. Can u post some code, so tat i can get a clear idea. Thanx in advance :)Morbilli
Possible duplicate of Android DownloadManager ProgressSixpack
Already answered this question https://mcmap.net/q/225499/-using-progressbar-with-downloadmanagerGermanium
S
5

This is the code which i used to show a progess bar in my activity and the download was requested by download manager.

final ProgressDialog progressBarDialog= new ProgressDialog(this);
progressBarDialog.setTitle("Download App Data, Please Wait");

              progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,
                    int whichButton){
                    // Toast.makeText(getBaseContext(),
                    //       "OK clicked!", Toast.LENGTH_SHORT).show();
                }
            });
            progressBarDialog.setProgress(0);

            new Thread(new Runnable() {

                @Override
                public void run() {

                    boolean downloading = true;

                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    while (downloading) {

                        DownloadManager.Query q = new DownloadManager.Query();
                        q.setFilterById(DownloadManagerId); //filter by id which you have receieved when reqesting download from download manager
                        Cursor cursor = manager.query(q);
                        cursor.moveToFirst();
                        int bytes_downloaded = cursor.getInt(cursor
                            .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                            downloading = false;
                }

                final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        progressBarDialog.setProgress((int) dl_progress);

                    }
                });

                // Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
                cursor.close();
            }

        }
    }).start();


  //show the dialog
        progressBarDialog.show();

          }

               }
Slyke answered 18/4, 2015 at 18:32 Comment(0)
D
0

i know its late to answer. but for the guys come in the future with same query,

The method given by nkalra0123 might not be safe i guess. There is something called, FileObserver.

Android DownloadManager Progress

This link mentioned above has better solution.

Dudek answered 4/8, 2015 at 12:55 Comment(2)
Thanks, but apparently the given solution stops working after Marshmallow.Octagonal
nkarla's solution works fine on Api 27 but not Api 29, when files are very large (e.g 50 MB is fine, but 300 mb is not).Largent

© 2022 - 2024 — McMap. All rights reserved.