Android update ui from handler every second
Asked Answered
P

2

9

I need a little help with updating my UI from Runnable/Handler every second. I'm using this code :

Runnable runnable = new Runnable() {
        @Override
        public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        prBar.setProgress(myProgress);
                        y = (double) ( (double) myProgress/ (double) RPCCommunicator.totalPackets)*100;
                        txtInfoSync1.setText(Integer.toString((int)y) + "%");
                        prBar.setMax(RPCCommunicator.totalPackets);

                        int tmp = totalBytesReceived - timerSaved;
                        Log.w("","totalBytesReceived : "+totalBytesReceived + " timerSaved : "+timerSaved );
                        Log.w("","tmp : "+tmp);

                        if (avgSpeedCalc.size() > 10)
                        {
                            avgSpeedCalc.remove(0);
                        }

                        avgSpeedCalc.add(tmp);

                        int x = 0;

                        for (int y=0;y<avgSpeedCalc.size();y++)
                        {
                            x += avgSpeedCalc.get(y);
                            Log.d("","x : "+x);
                        }

                        x = Math.round(x/avgSpeedCalc.size());
                        Log.e("","x : "+x);

                        timerSaved = totalBytesReceived;
                        txtSpeed.setText(Integer.toString(x));

                    }
                });
        }
    };

I tried with handler.postDelayed(runnable, 1000); in onCreate(), but the runnable never starts. Or even if I try with runnable.run(); , it's still not working.

Any ideas how can I start runnable/handler and update the ui every second?

Pancreatin answered 6/2, 2012 at 12:10 Comment(0)
K
33

Why are you creating a runnable in a runnable?

Try this:

// flag that should be set true if handler should stop
boolean mStopHandler = false;

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // do your stuff - don't create a new runnable here!
        if (!mStopHandler) {
            mHandler.postDelayed(this, 1000);
        }
    }
};

// start it with:
mHandler.post(runnable);
Kath answered 6/2, 2012 at 12:16 Comment(10)
and how can I stop it after that? I mean I need this to run only when I'm getting data over internet and I want to stop the runnable after activity finish or user click cancel button.Pancreatin
Actually handler.removeCallbacks(runnable); is doing the same thing.Pancreatin
Thats true, this is preferable if you delay is long, regarding the 1sec interval it doesn't make a real differenceKath
@Kath Your runnable will only run once! Right?! What is then the correct way to make it run forever?Prepared
@Prepared the current code will run forever. mStopHandler is false, so !mStopHandler is true and the postDelayed(this, 1000); will restart the runnable after 1 second.Kath
@Kath Don't you need to make runnable final?Prepared
@Prepared It depends where you put this code. If you define it outside a method as member variables (except the mHandler.post(runnable), of course), nothing of the used vars need to be final. If you put the complete code in a method, you must make mStopHandler and mHandler final.Kath
@Kath How can I restart the same handler after making mStophandler true, or do I need to create another ??Cicily
@IGOTSAR just make the flag false and call mHandler.post(runnable) againKath
can the above code be used to toggle between the states of toggle button..?Murial
H
5

If you want to update your UI i think this is best option

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do your work
            }
        },500);
Hyehyena answered 13/7, 2016 at 7:5 Comment(1)
can the above code be used to toggle between the states of toggle button..?Murial

© 2022 - 2024 — McMap. All rights reserved.