【问题标题】:How to receive status of download manager intent until download success or failed如何在下载成功或失败之前接收下载管理器意图的状态
【发布时间】:2018-12-13 12:32:13
【问题描述】:

这是我的问题。 我正在尝试通过 Asynctask 使用下载管理器意图从我的服务器下载文件。 在我的 asynctask 类的 doInBackground 中,我调用了下载管理器意图,并且 doinBackground 将在下载完成时返回布尔值(成功或失败)。 这是我的代码

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

但 DownloadManager 的状态永远不会在 DownloadManager.STATUS_SUCCESSFUL 或 DownloadManager.STATUS_FAILED 上跳转。

【问题讨论】:

    标签: android android-asynctask download-manager


    【解决方案1】:

    不需要 AsyncTask 或同步查询。 DownloadManager 已经是异步的。您应该为 ACTION_DOWNLOAD_COMPLETE 注册一个 BroadcastReceiver,以便在下载完成(或失败)时收到通知。

    http://blog.vogella.com/2011/06/14/android-downloadmanager-example有一个很好的例子

    【讨论】:

      【解决方案2】:

      您必须重新查询下载管理器。即使数据发生变化,光标也保持不变。试试这样:

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

      【讨论】:

        【解决方案3】:

        下载管理器以异步方式下载文件。所以无需将下载管理器放在 Asyntask 中。

        如果下载完成,您可以使用 Receiver 获取下载管理器的状态。

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

        不要忘记在Manifest

        中添加您的接收者
          <receiver
                android:name=".CheckDownloadComplete"
                android:enabled="true"
                android:exported="true" >
                <intent-filter>
                    <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                </intent-filter>
            </receiver>
        

        【讨论】:

        • 您好,我知道这是一篇旧帖子,但我想知道为什么在 CheckDownloadComplete 中检查操作是否等于 ACTION_DOWNLOAD_COMPLETE 虽然它已经在清单中?你可以在课堂上放下支票吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-09
        • 1970-01-01
        • 2021-12-03
        • 2020-04-21
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多