why is that?
while (flag) {
outCPU.setText(getCpuInfo());
}
getCpuInfo returns string, if I try to write this method's return out into a log, there is everything that should be, but nothing happens to textview..
why is that?
while (flag) {
outCPU.setText(getCpuInfo());
}
getCpuInfo returns string, if I try to write this method's return out into a log, there is everything that should be, but nothing happens to textview..
It will not work... display will update after your function finishes. Try this
boolean flag;
private void updateTextView(){
outCPU.setText(getCpuInfo());
if(flag){
outCPU.post(new Runnable(){
public void run(){
updateTextView();
}
});
}
}
private void your_function(){
if(flag){
outCPU.post(new Runnable(){
public void run(){
updateTextView();
}
});
}
}
The infinite loop on the ui thread it is not probably a good idea. setText schedule a draw operation, posting on the ui thread queue. Unfortunately the same thread is busy looping. You could use the TextView's internal handler to post a Runnable on the ui thread's queue. E.g.
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (!flag) {
outCPU.removeCallbacks(this);
return;
}
outCPU.setText(getCpuInfo());
outCPU.postDelayed(this, 200);
}
};
and in place of your while loop you simply do
outCPU.post(mRunnable);
if (!flag)
, though, as apparently that's the exit condition –
Donia © 2022 - 2024 — McMap. All rights reserved.
flag
so that loop ends? – CabanaoutCPU.setText(getCpuInfo());
– Laugh