java - Android: update GUI at timertask within service -


.

hello community,

i looked @ forum question don't know how use handler.

my problem:
gui has textfield. created service , works. service shall update ui.

what have:

  • android galaxy s handy
  • indigo service release 2

my coding:

public class samsungloc1 extends activity implements onclicklistener {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_samsung_loc1);      buttonstart = (button) findviewbyid(r.id.buttonstart);         buttonstop = (button) findviewbyid(r.id.buttonstop);          buttonstart.setonclicklistener(this);         buttonstop.setonclicklistener(this);          textview tv1 = (textview) findviewbyid(r.id.textview01);         tv1.settext("initial1");     }      public void onclick(view src) {         switch (src.getid()) {         case r.id.buttonstart:           startservice(new intent(this, myservice.class));           break;         case r.id.buttonstop:           log.d(tag, "onclick: stopping srvice");           break;         }       }  }    

so when clicking start button, service myservice.class starts:

public class myservice extends service {     private static final string tag = "myservice";     @override     public ibinder onbind(intent intent) {         return null;     }      @override     public void oncreate() {     }      @override     public void ondestroy() {     }      @override     public void onstart(intent intent, int startid) {           timer t = new timer();             t.scheduleatfixedrate(new timertask() { //              @override                 public void run() {  //here change textview of ui                              tv1.settext("new information");                    }                                },0,300000);          } } 

well, read must use handler, don't know how use it. can please please please :-) give me code snipped how change textview tv1 inside timertask of service?

kindly regards,
andy

the service shall update ui

no, shall not.

the service shall let ui layer of app know event occurred in background. bear in mind there might not be ui layer of app in foreground, if user has pressed or home or something.

and, if response "well, stopping service in situations", not need service , should rid of it. point behind service able run independently ui layer, pure background work.

the service myservice.class starts

onstart() has been deprecated 4 years. please learn modern android app development. use onstartcommand(), not onstart().

i read must use handler

while option, not first choice. or second choice. or third choice.

my personal first choice use third-party message bus, such otto. not have sample otto app handy, have not covered yet in book (though high on to-do list...).

my second choice use localbroadcastmanager. advantage of localbroadcastmanager is in android support package (which may using), , works lot regular system broadcasts (which may have experience with). here sample project using localbroadcastmanager.

my third choice use actual system broadcasts, service calls sendbroadcast() , activity has registered broadcastreceiver when in foreground. useful in cases might want update activity if in foreground, otherwise display notification, shown in this sample app. useful allowing third-party apps find out events, first 2 solutions preclude.

while there may scenarios in using handler directly idea, cannot think of relevant newcomer android.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -