2016-09-28 76 views
0

我用DownloadManager從服務器下載文件,我期望當網絡沒有連接到互聯網我收到STATUS_PAUSEDBroadcastReceiver。但它不叫onReceive()STATUS_PAUSED將不會調用DownloadManager中的onReceive()

downloadReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // ... 
    } 
} 

registerReceiver(downloadReceiver, 
    new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

回答

0

您正在篩選ACTION_DOWNLOAD_COMPLETE操作,您的接收器將不會收到任何其他廣播。並且,STATUS_PAUSED不是廣播。

這是您可以查詢的由DownloadManager管理的特定下載的狀態。

例如:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
DownloadManager.Query query = new DownloadManager.Query(); 

query.setFilterById(idsToQuery); 
query.setFilterByStatus(DownloadManager.STATUS_PAUSED); 

Cursor cursor = dm.query(query); 

if (cursor.moveToFirst()) { 
    // do whatever you would like with the result 
} 
0

至於Download Manager類而言,它不Broadcast關於下載內容的狀態的任何狀態。只有兩個狀態被廣播

  1. ACTION_DOWNLOAD_COMPLETE

  2. ACTION_NOTIFICATION_CLICKED

相關問題