2014-10-06 47 views
6

这是我的问题。 我试图通过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.STATUS_SUCCESSFUL或DownloadManager.STATUS_FAILED跳。 对我有帮助吗?

回答

4

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

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

下载管理器下载文件,一个很好的例子。所以不需要在Asyntask中放置下载管理器。

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

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

不要忘记添加接收机清单

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