How can download file in service in android -


i want download file server , want download in background such service.

my codes :

public class mainactivity extends appcompatactivity {      button download;     textview downloadcount;     progressbar progressbar;      future<file> downloading;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // enable global ion logging         ion.getdefault(this).configure().setlogging("ion-sample", log.debug);          setcontentview(r.layout.activity_main);          download = (button) findviewbyid(r.id.download);         downloadcount = (textview) findviewbyid(r.id.download_count);         progressbar = (progressbar) findviewbyid(r.id.progress);          file sdcard = environment.getexternalstoragedirectory();         file dir = new file(sdcard.getabsolutepath() + "/dir1/dir2");         if (!dir.exists()) {             dir.mkdirs();         }         final file file = new file(dir, "filename.zip");          download.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 if (downloading != null && !downloading.iscancelled()) {                     resetdownload();                     return;                 }                  download.settext("cancel");                 // 180mb zip file test                 downloading = ion.with(mainactivity.this)                         .load("http://cdn.p30download.com/?b=p30dl-software&f=pcwinsoft.1avmonitor.v1.9.1.50_p30download.com.rar")                         // attach percentage report progress bar.                         // can attach progressdialog progressdialog.                         .progressbar(progressbar)                         // callbacks on progress can happen on ui thread                         // via progresshandler. useful if need update textview.                         // updates textviews must happen on ui thread.                         .progresshandler(new progresscallback() {                             @override                             public void onprogress(long downloaded, long total) {                                 downloadcount.settext("" + downloaded + " / " + total);                             }                         })                         // write file                         .write(file)                         // run callback on completion                         .setcallback(new futurecallback<file>() {                             @override                             public void oncompleted(exception e, file result) {                                 resetdownload();                                 if (e != null) {                                     toast.maketext(mainactivity.this, "error downloading file", toast.length_long).show();                                     return;                                 }                                 toast.maketext(mainactivity.this, "file upload complete", toast.length_long).show();                             }                         });             }         });     }      void resetdownload() {         // cancel pending download         downloading.cancel();         downloading = null;          // reset ui         download.settext("download");         downloadcount.settext(null);         progressbar.setprogress(0);     } } 

i write above codes don't know how can write code service , download file in background.

how can write codes in service?

a service can started using context.startservice(intent) . once service started, it's lifecycle executed below (in sequence):

  1. oncreate()
  2. onstartcommand()

when service stopped or destroyed, it's ondestroy() method called.

now download file in service, call startservice() method below (from activity in case):

intent intent = new intent(mainactivity.this, servicename.class); startservice(intent); 

this start service. start download process, call downloadfile() method show below:

public class servicename extends service {     @override     public void oncreate() {         super.oncreate();         //start download process         downloadfile();     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         // want service continue running until explicitly         // stopped, return          return start_sticky;     }      @override     public void ondestroy() {         super.ondestroy();     }      private void downloadfile() {         //logic download file.         .         .         .     } } 

also can bind service activity pass callbacks between them , bind service w/ activity's lifecycle. checkout link more info on this.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -