Search code examples
androidandroid-download-manager

timeout for android DownloadManager


I'm using android build-in DownloadManager to download files from internet. Problem is once i enqueued a download, it tries to download the file forever!! is there any way to set a timeout for downloads?


Solution

  • Unfortunately Android dosen't puropose a solution to set timeout in DownloadManager But in fact You can set a TIMEOUT ClockWake when you are in case of state of pending : DownloadManager.STATUS_PENDING

    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: 
        //here you can set your TIMEOUT solution
        break; 
        case DownloadManager.STATUS_RUNNING: 
        break; 
        case DownloadManager.STATUS_SUCCESSFUL: 
        break; 
        case DownloadManager.STATUS_FAILED: 
        break; 
        }