java - Android Handler and Thread at start time not running? -


i'm trying create handler @ start of application can have 2 threads working 1 ui , 2 server, i'm doing server not stop ui lagging, , usefully sort out lag issues, anyways, i'm looking @ website http://crodrigues.com/updating-the-ui-from-a-background-thread-on-android/ , guy creates runnable method, run method, there method called updategame called when method ran, have tried out code so

public class mainactivity extends activity {  private static final string tag = gameobject.class.getsimplename(); //create handler deal server private handler serverhandler = new handler();  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     //turn off title     requestwindowfeature(window.feature_no_title);     //make application full screen     getwindow().setflags(             windowmanager.layoutparams.flag_fullscreen,             windowmanager.layoutparams.flag_fullscreen);      setcontentview( new gamepanel( ) );     log.d( tag, "view added" );      //server method     new thread(new runnable() { onserver( ); } ).start( ); }  final runnable updaterunnable = new runnable() {     public void run() {         //call activity method updates ui         updategame();     } };  //give positions game public void updategame() {     log.d(tag, "update game"); }  //update/run server private void onserver() {     if( gamepanel.rtnserverstate() == true )     {         log.d(tag, "start server");     }      serverhandler.post( updaterunnable ); }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.activity_main, menu);     return true; }  public void ondestroy() {     log.d( tag,  "destroying... " );     super.ondestroy(); }  public void onstop() {     log.d( tag,  "stopping... " );     super.onstop(); } } 

and updategame ran once. can see issue in why doesn't keep running in background?

canvas

updated post

public class mainactivity extends activity {  private static final string tag = gameobject.class.getsimplename(); private final handler serverhandler = new handler();  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     //turn off title     requestwindowfeature(window.feature_no_title);     //make application full screen     getwindow().setflags(             windowmanager.layoutparams.flag_fullscreen,             windowmanager.layoutparams.flag_fullscreen);      setcontentview( new gamepanel( ) );      textview textview = new textview(this);     textview.settextsize(40);     string message = "hello";     textview.settext(message);      log.d( tag, "view added" );     //server method     new thread(new runnable() {       @override     public void run() { onserver( ); } } ).start( ); }  private void updateserver() {     log.d(tag, "testing"); }   //update/run server private void onserver() {     if( gamepanel.rtnserverstate() == true )     {         log.d(tag, "start server");     }      serverhandler.post( updaterunnable ); }  //update/server final runnable updaterunnable = new runnable() {     public boolean running = true;     public void run() {         while(running){             //call activity method updates ui             updateserver();         }     } };  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.activity_main, menu);     return true; }  public void ondestroy() {     log.d( tag,  "destroying... " );     super.ondestroy(); }  public void onstop() {     log.d( tag,  "stopping... " );     super.onstop(); } } 

update number 2

public class mainactivity extends activity {  private static final string tag = gameobject.class.getsimplename(); private final handler serverhandler = new handler();  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     //turn off title     requestwindowfeature(window.feature_no_title);     //make application full screen     getwindow().setflags(             windowmanager.layoutparams.flag_fullscreen,             windowmanager.layoutparams.flag_fullscreen);      setcontentview( new gamepanel( ) );      textview textview = new textview(this);     textview.settextsize(40);     string message = "hello";     textview.settext(message);      log.d( tag, "view added" );     //server method     runnable server = new runnable() {         public boolean running = true;         public void run() {             while(running){                 onserver();  // make sure blocks in way             }         }     }; }  private void updateserver() {     log.d(tag, "testing"); }   //update/run server private void onserver() {     if( gamepanel.rtnserverstate() == true )     {         log.d(tag, "start server");     }      serverhandler.post( updaterunnable ); }  //update/server final runnable updaterunnable = new runnable() {     public void run() {         //call activity method updates ui         updateserver();     } };  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.activity_main, menu);     return true; }  public void ondestroy() {     log.d( tag,  "destroying... " );     super.ondestroy(); }  public void onstop() {     log.d( tag,  "stopping... " );     super.onstop(); } } 

the runnable object's run method called once, after new thread created in response .start() call.

usually like:

final runnable myrunnable = new runnable() {     public boolean running = true;     public void run() {         while(running){             dosomething();         }     } }; 

but i'm not sure best way this. updategame() method called unnecessarily.

instead, put server logic inside runnable's run() method. in there use while(running){...} construct listed above make sure there blocking call in there. whether network socket, blockingqueue, etc. way won't needlessly loop.


edit

from discussion in comments. leave

final runnable updaterunnable = new runnable() {     public void run() {         //call activity method updates ui         updategame();     } }; 

as , change

new thread(new runnable() { onserver( ); } ).start( ); 

to

runnable server = new runnable() {     public boolean running = true;     public void run() {         while(running){             onserver();  // make sure blocks in way         }     } } new thread(server).start(); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -