8

我有例如按钮,当用户单击它时,我想通过 DownloadManager 查找下载历史记录,但问题是我的光标为空,它永远不会进入条件 if(c.moveToFirst()),它总是跳过这个条件.

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

我也尝试了这段代码(由 imran khan 编写)但仍然是同样的问题:

DownloadManager.Query query = null;
    Cursor c = null;
    DownloadManager downloadManager = null;
    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
    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; 
    }
}
4

0 回答 0