java - How much of this code should be inside the UI Thread? -
so have asynctask
doing work in background. work list of commands done in loop. commands may run following method (inside loop):
public void print() { // create , add textview // variable "c" activity reference, in constructor passing "this" activity textview tv = new textview(c); // set text tv.settext(text); // set style tv.settextcolor(color.white); tv.settextsize(fontsize); tv.setsingleline(false); // add linear layout display.addview(tv); // add spacer view spacer = new view(c); // set bg color spacer.setbackgroundcolor(color.argb(0x88, 0, 0, 0)); // set width , height spacer.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, separatorheight)); // add layout display.addview(spacer); // screen should update displaychanged = true; }
at end of loop have line:
if (displaychanged) updatescreen();
and updatescreen()
method:
private void updatescreen() { // surround linear layout vertical scroll view scrollview scrollview = new scrollview(c); scrollview.setverticalscrollbarenabled(false); scrollview.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent)); scrollview.addview(display); // set code background color scrollview.setbackgroundcolor(color.rgb(0xaa, 0xaa, 0xaa)); // finally, display c.setcontentview(scrollview); // screen shouldn't update again displaychanged = false; }
as can see, both methods (print
, updatescreen
) work views , updatescreen
setcontentview
. want run of code in asynctask
background thread. how of code should in runonuithread(... code here ...)
?
you can set displaychanged
in background thread. rest of code uses ui toolkit , accessing in uithread considered unsafe , not recommended google according the documentation.
Comments
Post a Comment