0

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?

  • 3
    `View`s are drawn, optimally, every 16ms (or at least attempted to unless you get jank), so updating a view every 5ms is pointless. Also posting a runnable does not guarantee immediate execution and should not be relied upon as accurate measurement of time. – Mark Apr 07 '19 at 13:20
  • @MarkKeen, what is the solution? How should I resolve this problem? – Himanshu Agrawal Apr 07 '19 at 13:22
  • 1
    the easy solution would be to think that Android draws 60fps (frames of 16ms), your counter should not be tied to your runnable - probably should be on a separate thread looping with a sleep - they should be independent of each other. Your runnable should your get the value every 16+ms to update the view. If you choose to use concurrency the value should be, least volatile or synchronised in some way, possibly `AtomicInteger` – Mark Apr 07 '19 at 13:28

0 Answers0