Update UI every 1 second only updates every 4? Android Studio

1

I'm working on Android Studio and I have the following problem and I do not know why it happens: My code takes the current unix time in milliseconds, divides the unix time by 1000 to extract the seconds, subtracts a date in unix in seconds and shows the result. I did a timer that does it every second, but in the UI the number is only updated every ~ 4 seconds instead of every second as in the console.

I thought maybe it has to do with android, maybe it has a UI update limit per second? I leave the code below:

        Timer EachSec = new Timer();            //Creates a timer with 1s delay
    TimerTask Refresh = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread( new TimerTask(){
                @Override
                public void run() {
                    long AnDate = 1476650909;       //October 16th 2016, 5:48PM in seconds Unix
                    TextView SecondsText = (TextView) findViewById(R.id.SecondsText);   //links TextViewers with objects
                    TextView MinutesText = (TextView) findViewById(R.id.MinutesText);   //links TextViewers with objects
                    TextView HoursText = (TextView) findViewById(R.id.HoursText);       //links TextViewers with objects
                    TextView DaysText = (TextView) findViewById(R.id.DaysText);         //links TextViewers with objects
                    TextView MonthText = (TextView) findViewById(R.id.MonthText);
                    long SecondsTime = System.currentTimeMillis()/1000 - AnDate;        //Gets current time in milliseconds, pass it to seconds and subtracts AnDate, now having elapsed time in s
                    float MinutesTime = SecondsTime/60;     //pass it to minutes
                    float HoursTime = MinutesTime/60;       //pass it to hours
                    float DaysTime = HoursTime/24;          //pass it to days
                    float MonthTime = DaysTime/30;          //pass it to roughly months
                    String SecSt = String.valueOf(Math.round(SecondsTime)) + " " + getString(R.string.secondsSTR);      //transforms seconds to string and concatenates with the word and saves it into a string variable
                    String MinSt = String.valueOf(Math.round(MinutesTime)) + " " + getString(R.string.minutes);         //transforms minutes to string and concatenates with the word and saves it into a string variable
                    String HourSt = String.valueOf(Math.round(HoursTime)) + " "+  getString(R.string.hours);             //transforms hours to string and concatenates with the word and saves it into a string variable
                    String DaySt = String.valueOf(Math.round(DaysTime)) + " " + getString(R.string.days);                //transforms days to string and concatenates with the word and saves it into a string variable
                    String MonSt = String.valueOf(Math.round(MonthTime)) + " " + getString(R.string.months);
                    SecondsText.setText(SecSt);         //sets TextView text to concatenated string
                    MinutesText.setText(MinSt);         //sets TextView text to concatenated string
                    HoursText.setText(HourSt);          //sets TextView text to concatenated string
                    DaysText.setText(DaySt);            //sets TextView text to concatenated string
                    MonthText.setText(MonSt);           //sets TextView text to concatenated string
                    System.out.println(System.currentTimeMillis()/1000 - AnDate);
                }
            });
        }
    };
    EachSec.schedule(Refresh, 0, 1000);     //1 second

Thank you very much!

    
asked by Mauro Avalos 14.10.2018 в 21:05
source

1 answer

0

Apparently the problem is that before showing it, I applied Math.round(SeconsTime) so the value is the same for about 4 seconds. I discovered it after trying to put System.out.println(Math.round(System.currentTimeMillis()/1000 - AnDate)); and realize that the value is the same for a while.

    
answered by 14.10.2018 в 21:13