How to get status of downloading?
Asked Answered
K

2

8

I use DownloadManager for getting status of downloading, but it still doesn't work, it never jumps to condition if(c.moveToFirst()) and I don't know why. Could anybody help me, please?

private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(Intent.ACTION_SCREEN_OFF.equals(action)) {

                DownloadManager downloadMgr = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
                Cursor c = downloadMgr.query(query);
                if(c==null) {
                    //
                }
                else {
                    if(c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int status = c.getInt(columnIndex);
                        if(status == DownloadManager.STATUS_RUNNING){
                        //do something
                        }
                    }
                }
            }
        }
    };
Katakana answered 21/4, 2012 at 11:11 Comment(9)
Obviously your cursor is empty?Hypodermic
It seems that cursor is not empty, because it always go to else, not to if(c==null).Katakana
(null != empty). I'm not sure what your code is trying to do, but it just looks like nothing is being downloaded at the time of the queryHypodermic
I use this code in broadcastreceiver which is called when user turns his screen off, then it should call this code and tell me if user is downloading some data or not.Katakana
You are right, my cursor is really empty, but I don't know why.Katakana
@Katakana : post your full code u are passing a file for downloading or want to get download folder statsDias
Look at edited question, I only need to get folder stats.Katakana
Then it must be @Katakana that your query is wrong.Hypodermic
And how can I fix it? I have spent lots of time on this issue and I'd like to solve it.Katakana
S
9

Here are few link refer it.

sample code is below::

DownloadManager.Query query = null;
    Cursor c = null;
    DownloadManager downloadManager = null;
    downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
     if(query!=null) {
                query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                        DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
            } else {
                return;
            }
    c = downloadManager.query(query);
    if(c.moveToFirst()) { 
    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 
    switch(status) { 
    case DownloadManager.STATUS_PAUSED: 
    break; 
    case DownloadManager.STATUS_PENDING: 
    break; 
    case DownloadManager.STATUS_RUNNING: 
    break; 
    case DownloadManager.STATUS_SUCCESSFUL: 
    break; 
    case DownloadManager.STATUS_FAILED: 
    break; 
    }
}
Strained answered 25/4, 2012 at 2:31 Comment(1)
We are seeing some status codes of 0. How is this possible?Exarate
H
1

This is how I do it

//broadcastReceiver
private val receiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)

      
        //call notify off NotificationManager passing in the id & notification
        notificationManager.apply {

            notify(NOTIFICATION_ID, createNotification())
        }


        //DownloadManager.Query() is used to filter DownloadManager queries
        val query = DownloadManager.Query()

        query.setFilterById(id)

        val cursor = downloadManager.query(query)

        if (cursor.moveToFirst()){
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))

            when (status) {
                DownloadManager.STATUS_SUCCESSFUL ->{


                }
                DownloadManager.STATUS_FAILED -> {
                    
                }

            }
        }

    }
}
Holcombe answered 10/2, 2021 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.