2012-04-21 40 views
6

Uso DownloadManager per ottenere lo stato di download, ma ancora non funziona, non salta mai alla condizione if(c.moveToFirst()) e non so perché. Qualcuno potrebbe aiutarmi, per favore?Come ottenere lo stato di download?

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

Ovviamente il cursore è vuota? – Blundell

+0

Hm? E come posso risolverlo? – Adam

+0

Sembra che il cursore non sia vuoto, perché va sempre in altro, non in se (c == null). – Adam

risposta

7

Di seguito sono riportati alcuni collegamenti.

codice di esempio è al di sotto ::

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

Stiamo visualizzando alcuni codici di stato pari a 0. Come è possibile? – fobbymaster

Problemi correlati