I have created two TextViews and updating their contents through two different handlers, one for each TextView. I am incremented two counters in both handlers, one in each, and posting their values to respective TextView.
My first handler is ticking according to the value of long speed variable, and second handler is ticking 10 times later.
public class Main extends Activity{
private View root;
private final Context context = this;
private int count1 = 0, count2 = 0;
private TextView view1, view2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
setContentView(R.layout.s_main);
root = findViewById(R.id.root);
view1 = findViewById(R.id.txv1);
view2 = findViewById(R.id.txv2);
//
ViewTreeObserver vto = root.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
root.getViewTreeObserver().removeOnPreDrawListener(this);
startRuns();
return true;
}
});
}
private void startRuns() {
try {
final long sleep = 10;
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
view1.setText(String.valueOf(count1++));
handler1.postDelayed(this, sleep);
}
}, 1);
final Handler handler2 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
view2.setText(String.valueOf(count2++));
handler2.postDelayed(this, sleep * 10);
}
}, 1);
Threads.run = true;
} catch (Exception ex) {
Alerts.alert(context, ex.getMessage());
}
}
}
This is good till the value of speed is 10 or above, but as soon i set the value of speed to 1, both handlers work with same duration or speed. First handler should work with speed of 1, and second handler should work with speed of 10, but both works with speed of 10 ms.
EDIT
In short, minimum delay on which postDelayed is working is 10 ms, not less than that, not 1, not 5, at least 10. I need to run a runnable with 5 ms delay in a project.
Can you please suggest where I am wrong?