android - DownloadMAnager Broadcast only DOWNLOAD_COMPLETE -


to improve comunication on different commponents want listen downloadmanager status. following reciever download_complete(success/failure) only,while alson need status pending, running, paused

public class downloadreceiver extends broadcastreceiver{      private context context;     private string tag = "downloadreceiver";      @override     public void onreceive(context context, intent intent){         log.i(tag, "onreceive");         this.context = context;         long downloadid = intent.getlongextra(downloadmanager.extra_download_id, -1);         log.i(tag, "downloader id: "+downloadid);          checkdownloadstatus(downloadid);     }       private void checkdownloadstatus(long id){         downloadmanager downloadmanager = (downloadmanager) context.getsystemservice(                                                                                         context.download_service         );         cursor cursor = context.getcontentresolver()                                .query(                                          downloadstateprovider.content_uri,                                          new string[]{downloadstatetable.column_id},                                          downloadstatetable.column_download_id + " =? ",                                          new string[]{"" + id}, null                                );         if(cursor.movetofirst()){             log.i(tag, "record exist");              // todo auto-generated method stub             downloadmanager.query query = new downloadmanager.query();             query.setfilterbyid(id);             cursor dmcursor = downloadmanager.query(query);              if(dmcursor.movetofirst()){                 log.i(tag, "download exist");                  int columnindex = dmcursor.getcolumnindex(downloadmanager.column_status);                 int status = dmcursor.getint(columnindex);                 int columnreason = dmcursor.getcolumnindex(downloadmanager.column_reason);                 int reason = dmcursor.getint(columnreason);                 dmcursor.close();                 string reasontext = "";                 contentvalues values = new contentvalues();                 switch(status){                     case downloadmanager.status_failed:                         switch(reason){                             case downloadmanager.error_cannot_resume:                                 reasontext = "error_cannot_resume";                                 break;                             case downloadmanager.error_device_not_found:                                 reasontext = "error_device_not_found";                                 break;                             case downloadmanager.error_file_already_exists:                                 reasontext = "error_file_already_exists";                                 break;                             case downloadmanager.error_file_error:                                 reasontext = "error_file_error";                                 break;                             case downloadmanager.error_http_data_error:                                 reasontext = "error_http_data_error";                                 break;                             case downloadmanager.error_insufficient_space:                                 reasontext = "error_insufficient_space";                                 break;                             case downloadmanager.error_too_many_redirects:                                 reasontext = "error_too_many_redirects";                                 break;                             case downloadmanager.error_unhandled_http_code:                                 reasontext = "error_unhandled_http_code";                                 break;                             case downloadmanager.error_unknown:                                 reasontext = "error_unknown";                                 break;                         }                         values.put(downloadstatetable.column_status,4);                         values.put(downloadstatetable.column_reason,reasontext);                         break;                     case downloadmanager.status_paused:                         switch(reason){                             case downloadmanager.paused_queued_for_wifi:                                 reasontext = "paused_queued_for_wifi";                                 break;                             case downloadmanager.paused_unknown:                                 reasontext = "paused_unknown";                                 break;                             case downloadmanager.paused_waiting_for_network:                                 reasontext = "paused_waiting_for_network";                                 break;                             case downloadmanager.paused_waiting_to_retry:                                 reasontext = "paused_waiting_to_retry";                                 break;                         }                         values.put(downloadstatetable.column_status, 2);                         values.put(downloadstatetable.column_reason, reasontext);                         break;                     case downloadmanager.status_pending:                         values.put(downloadstatetable.column_status, 0);                         values.put(downloadstatetable.column_reason, reasontext);                         break;                     case downloadmanager.status_running:                         values.put(downloadstatetable.column_status, 1);                         values.put(downloadstatetable.column_reason, reasontext);                         break;                     case downloadmanager.status_successful:                         values.put(downloadstatetable.column_status, 3);                          values.put(downloadstatetable.column_reason, reasontext);                         break;                 }                 log.i(tag, "status: "+status+", reason: "+reason);                  context.getcontentresolver().update(downloadstateprovider.content_uri,values,null,null);             }else log.i(tag, "download exist");          } else toast.maketext(context, "saved content error\nquery  empty", toast.length_short)                     .show();         cursor.close();     }    }    downloadreceiver = new downloadreceiver(); registerreceiver(downloadreceiver, new intentfilter(downloadmanager.action_download_complete)); 


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -