How to receive status of download manager intent until download success or failed

好久不见. 提交于 2019-12-08 15:47:10

问题


Here's my problem. I'm trying to download file from my server using download manager intent via Asynctask. in my doInBackground of asynctask class, i was call download manager intent, and doinBackground will return boolean value when download finish (Success or Failed). Here's my code

  protected Boolean doInBackground(String... f_url) {
        boolean flag = true;
        boolean downloading =true;
        try{
            DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
                           Request mRqRequest = new Request(
                                   Uri.parse("http://"+model.getDownloadURL()));
                           long idDownLoad=mManager.enqueue(mRqRequest);
                           DownloadManager.Query query = null;
                           query = new DownloadManager.Query();
                           Cursor c = null;
                             if(query!=null) {
                                        query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
                                                DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
                             } else {
                        return flag;
                            }
                             c = mManager.query(query);
                                if(c.moveToFirst()) { 
            int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 


                               while (downloading)
                               {    Log.i ("FLAG","Downloading");
                                   if (status==DownloadManager.STATUS_SUCCESSFUL)
                                   {    Log.i ("FLAG","done");
                                       downloading = false;
                                       flag=true;
                                       break;      
                                   }
                                   if (status==DownloadManager.STATUS_FAILED)
                                   {Log.i ("FLAG","Fail");
                                       downloading = false;
                                       flag=false;
                                      break;
                                   }
                       c.moveToFirst();
                               }
        }
                            return flag;
        }
        catch (Exception e)
        {
             flag = false;
                return flag;
        }    
    }

But DownloadManager status never jump on DownloadManager.STATUS_SUCCESSFUL or DownloadManager.STATUS_FAILED.


回答1:


You have to requery the download manager. The cursor stays the same even if the data changes. Try like this:

protected Boolean doInBackground(String... f_url) {
    boolean flag = true;
    boolean downloading =true;
    try{
        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);            
        Request mRqRequest = new Request(
        Uri.parse("http://"+model.getDownloadURL()));
        long idDownLoad=mManager.enqueue(mRqRequest);
        DownloadManager.Query query = null;
        query = new DownloadManager.Query();
        Cursor c = null;
        if(query!=null) {
            query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);                                         
        } else {
            return flag;
        }

        while (downloading) {
            c = mManager.query(query);
            if(c.moveToFirst()) { 
                Log.i ("FLAG","Downloading");
                int status =c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 

                if (status==DownloadManager.STATUS_SUCCESSFUL) {
                    Log.i ("FLAG","done");
                    downloading = false;
                    flag=true;
                    break;      
                }
                if (status==DownloadManager.STATUS_FAILED) {
                    Log.i ("FLAG","Fail");
                    downloading = false;
                    flag=false;
                    break;
                }
            }
        }

        return flag;
    }catch (Exception e) {
        flag = false;
        return flag;
    }    
}



回答2:


There's no need for the AsyncTask or the synchronous query. DownloadManager is already asynchronous. You should register a BroadcastReceiver for ACTION_DOWNLOAD_COMPLETE so that you get notified when the download completes (or fails).

There's a very good example at http://blog.vogella.com/2011/06/14/android-downloadmanager-example




回答3:


Download Manager download files in asynchronous manner. So no need to put download manager inside an Asyntask.

You can use Receiver for get the status of download manager if download complete.

    public class CheckDownloadComplete extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

         String action = intent.getAction();
            if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) 
            {
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
                DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                Cursor cursor = manager.query(query);
                if (cursor.moveToFirst()) {
                    if (cursor.getCount() > 0) {

                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        Long download_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);

                        // status contain Download Status
                        // download_id contain current download reference id

                        if (status == DownloadManager.STATUS_SUCCESSFUL)
                        {
                            String file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));

                            //file contains downloaded file name

                            // do your stuff here on download success

                        } 
                    }
                }
                cursor.close();
            }   
    }
}

Dont forget to add your receiver in Manifest

  <receiver
        android:name=".CheckDownloadComplete"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
    </receiver>


来源:https://stackoverflow.com/questions/26210048/how-to-receive-status-of-download-manager-intent-until-download-success-or-faile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!