android - Detect when system starts a download -
is there way detect when system starts download , information of , force download specific location? googled , results not helpful
start service class , start download print result status of download.
public class myservice extends service {
private timer timer; private timertask timertask; private static final int sampling_rate = 1000; public myservice() { } @override public ibinder onbind(intent intent) { return null; } @override @deprecated public void onstart(intent intent, int startid) { log.e("download", "onstart"); } @override public void oncreate() { timer = new timer(); timertask = new timertask() { @override public void run() { // log.d(myservice.class.tostring(), // "tic ... "+system.currenttimemillis()); getdownloaddata(); } }; if (timer != null && timertask != null) { timer.schedule(timertask, 0, sampling_rate); } } @targetapi(build.version_codes.gingerbread) @suppresslint("newapi") public void getdownloaddata() { 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) { system.out.println("--------------------------"); } else { if (c.movetofirst()) { system.out.println("------------end--------------"); while (c.isafterlast() == false) { getstatus(c); c.movetonext(); } } } } public void getstatus(cursor c) { int columnindex = c.getcolumnindex(downloadmanager.column_status); int columnreason = c.getcolumnindex(downloadmanager.column_reason); int reason = c.getint(columnreason); int status = c.getint(columnindex); string statustext = null; string reasontext = null; switch (status) { case downloadmanager.status_failed: statustext = "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; } break; case downloadmanager.status_paused: statustext = "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; } break; case downloadmanager.status_pending: statustext = "status_pending"; break; case downloadmanager.status_running: statustext = "status_running"; break; case downloadmanager.status_successful: statustext = "status_successful"; break; } log.d("status", statustext + " " + reasontext); }
}
Comments
Post a Comment