Android intents and services -


i'm having requirement of designing update service application, update service pull data clued , should update gui if there new updates. first part straight forward second part best approach?

i'm thinking of having custom intent receiver registered in application , tell activities load content again. if application closed need show custom dialog activity telling updates.

i need feedbacks , if body having similar sample project please share @ implementation details.

thanks.

this update service pull/download data clued

download service

the big question here is: how update activity service?. in next example going use 2 classes may not aware of: resultreceiver , intentservice. resultreceiver 1 allow update our thread service; intentservice subclass of service spawns thread background work there (you should know service runs in same thread of app; when extends service, must manually spawn new threads run cpu blocking operations).

download service can this:

public class downloadservice extends intentservice {     public static final int update_progress = 8344;     public downloadservice() {         super("downloadservice");     }     @override     protected void onhandleintent(intent intent) {         string urltodownload = intent.getstringextra("url");         resultreceiver receiver = (resultreceiver) intent.getparcelableextra("receiver");         try {             url url = new url(urltodownload);             urlconnection connection = url.openconnection();             connection.connect();             // useful can show typical 0-100% progress bar             int filelength = connection.getcontentlength();              // download file             inputstream input = new bufferedinputstream(url.openstream());             outputstream output = new fileoutputstream("/sdcard/barcodescanner-debug.apk");              byte data[] = new byte[1024];             long total = 0;             int count;             while ((count = input.read(data)) != -1) {                 total += count;                 // publishing progress....                 bundle resultdata = new bundle();                 resultdata.putint("progress" ,(int) (total * 100 / filelength));                 receiver.send(update_progress, resultdata);                 output.write(data, 0, count);             }              output.flush();             output.close();             input.close();         } catch (ioexception e) {             e.printstacktrace();         }          bundle resultdata = new bundle();         resultdata.putint("progress" ,100);         receiver.send(update_progress, resultdata);     } } 

add service manifest:

<service android:name=".downloadservice"/> 

it should update gui if there new updates

the activity this:

// initialize progress dialog in first example

// how fire downloader

mprogressdialog.show(); intent intent = new intent(this, downloadservice.class); intent.putextra("url", "url of file download"); intent.putextra("receiver", new downloadreceiver(new handler())); startservice(intent); 

here resultreceiver comes play:

private class downloadreceiver extends resultreceiver{     public downloadreceiver(handler handler) {         super(handler);     }      @override     protected void onreceiveresult(int resultcode, bundle resultdata) {         super.onreceiveresult(resultcode, resultdata);         if (resultcode == downloadservice.update_progress) {             int progress = resultdata.getint("progress");             mprogressdialog.setprogress(progress);             if (progress == 100) {                 mprogressdialog.dismiss();             }         }     } } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -