0

In one click i added:

tStart = System.currentTimeMillis();

In the second click button event i added:

long tEnd = System.currentTimeMillis();
long tDelta = tEnd - tStart;
double elapsedSeconds = tDelta / 1000.0;
timerValue.setText("00:00:" + tDelta);

tStart is global long var.

And timerValue is a TextView also global.

The problem is that i'm getting on the seconds of timerValue when doing setText some very long number for example: 1442257716372 and i clicked on the second button after a second or two.

Another problem is that if the time between the clicks is more then 60 seconds how do i update the minutes ?

The Runnable i'm calculating in:

Runnable serverChecksRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            if (connectedSuccess == true)
            {
                    checkServer = Get(iptouse + "uploadstatus");

            }

            Handler h=new Handler(Looper.getMainLooper());
            h.post(new Runnable()
            {
                @Override
                public void run()
                {
                    if (connectedSuccess)
                    {
                        if (checkServer != null)
                        {
                            long tStart;
                            String a = null;
                            try
                            {
                                a = new String(checkServer, "UTF-8");
                                textforthespeacch = a;
                                if (textforthespeacch.contains("upload completed"))
                                {
                                    String varr = textforthespeacch.substring(17);
                                    String varr1 = textforthespeacch.substring(0, 16);
                                    textforthespeacch = varr1;
                                    status1.setText("Upload completed" + " " + varr + "%");
                                    long tEnd = System.currentTimeMillis();
                                    long tDelta = tEnd - tStart;
                                    double elapsedSeconds = tDelta / 1000.0;
                                    timerValue.setText("00:00:" + elapsedSeconds);
                                    numberofuploadedfilescounter += 1;
                                    uploadedfilescount.setText(("Uploaded Files: " + numberofuploadedfilescounter));
                                    MainActivity.this.initTTS();
                                }
                                if (textforthespeacch.contains("uploading"))
                                {
                                    String[] split = textforthespeacch.split(" ");
                                    textforthespeacch = split[0];
                                    status1.setText("Uploading" + " " + split[1] + "%");
                                    tStart = System.currentTimeMillis();
                                    servercheckCounter += 1;
                                    if (servercheckCounter == 1)
                                    {
                                        MainActivity.this.initTTS();
                                    }
                                }
                            } catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            });

            customHandler.postDelayed(serverChecksRunnable,1000);
        }
    };

What should i init tStart to ? I did first time long tStart = 0;

Now i removed the 0 but then i'm getting error on the tStart later in the code say that the variable is not initialized.

The reason i moved the tStart to the top of the Runnable is that the tStart is not in the same place of the tDelta and the tEnd.

Daniel van wolf
  • 393
  • 1
  • 5
  • 16
  • You used tDelta instead of elapsedSeconds: timerValue.setText("00:00:" + tDelta); – Pedro Affonso Sep 14 '15 at 19:15
  • And for your second problem check number of seconds. If number of seconds is greater than 60 i.e. elapsedSeconds>60, convert it to minutes accordingly – Rahul Sep 14 '15 at 19:17
  • Pedro i tried for testing using the elapsedSeconds only on seconds ot see if this part only is working what i get on as result is 00:00:1.44226600723E9 maybe i should not using double at all and display only one digit after the number for example if seconds then 1.7s or 6.5s – Daniel van wolf Sep 14 '15 at 21:29

2 Answers2

1

For better formatting, you can use:

double secs = (tDelta /1000.0) % 60;
int mins = (int)Math.floor(tDelta /(1000*60));
String text = String.format("00:%02d:%06.3f", mins, secs);
Pedro Affonso
  • 1,656
  • 15
  • 26
1

Using the answer by @bobince to this other question, you can do this:

long s = (System.currentTimeMillis() - tStart) / 1000;
timerValue.setText(String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60)));
Community
  • 1
  • 1
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • cybersam when using your code i'm getting this for example after 1-2 seconds: 400629:20:27 something wrong. Maybe tEnd and tStart not shuold use System.currentTimeMillis(); ? – Daniel van wolf Sep 14 '15 at 21:22
  • You right i initialized the variable tStart at the top of the Runnable to 0. Now i removed the 0 but then i'm getting error that tStart is not initialized. The reason i moved the tStart to the top of the Runnable is that the tStart is not in the same place of the tDelta and the tEnd. – Daniel van wolf Sep 14 '15 at 21:42
  • Looks like `textforthespeacch` does not contain "uploading" when you expect it to (and/or when your Runnable happens to be active -- every second or so). Fixing *that* issue would require understanding your app and the server API. I think you need to try to determine that on your own (or, failing that, it needs another Question). But I think your original Question (about setting the TextView timestamp) has been answered. – cybersam Sep 14 '15 at 21:53