Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6156057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:32:42+00:00 2026-05-23T20:32:42+00:00

Here is my timer class, This class is designed to constantly update a timer

  • 0

Here is my timer class, This class is designed to constantly update a timer in a view. However, when I run the app the first toast message is displayed to the screen but the second one is never reached (the timerTask’s “run” method is never executed). I know that this is probably something simple that I am doing wrong. If anyone could steer me in the right direcion that would be great.

public class MyTimer  { 

static Timer _timerTask = new Timer();
static int totalSeconds = 1, hour = 0, min = 0, sec = 0;
static String mTimeFormat = "%02d:%02d:%02d";
static String timeTakenString;



public static void start (){
    Toast.makeText(GPSMain.context, "Message one", Toast.LENGTH_LONG).show();

    TimerTask timer = new TimerTask() {
        @Override
        public void run() {
            Toast.makeText(GPSMain.context, "Message two", Toast.LENGTH_LONG).show();
            totalSeconds += 1;
            sec += 1;
            if(sec >= 60) {
                sec = 0;
                min += 1;
                if (min >= 60) {
                    min = 0;
                    hour += 1;
                }
            }
            timeTakenString = String.format(mTimeFormat, hour, min, sec);
            postExecute.sendEmptyMessage(0); //update UI
        }
        private Handler postExecute = new Handler(){
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                GPSMain.timer.setText("Time Taken: "+timeTakenString);
            }
        };
    };
_timerTask.scheduleAtFixedRate(timer,1000,1000);
}
}

code in another file calling this class:

MyTimer myTimer = new MyTimer();
....
myTimer.start();

PROJECT SPEC CHANGED!
My project leader changed the spec of the project so that it no longer needs to update the timer to the UI but rather display it as an end result. Accepting the first answer anyway as it solves the original problem. Will post the new code below.

New code calls:

 System.currentTimeMillis();

at the beggining and end of the runcycle, which returns a long. The first value is then subtracted from the second value to calculate the amount of time taken to execute the runcycle. That value is then manipulated and put into a timer format that is displayed at the end as a string.

public static String getTimeTaken(long end, long start){
    @SuppressWarnings("unused")
    String formattedTime = "", hourHour = "", hourMin = ":", minSec = ":";
    long timeTaken = (end-start)/1000, hour = 0, min = 0, sec = 0;
    if (timeTaken>9 ){
        hourHour = "0";
        hourMin = ":0";
        if (timeTaken>=60){
            if (timeTaken>= 3200){
                hour = timeTaken/3200;
                timeTaken = timeTaken%3200;
                if (hour>9){
                    hourHour = "";
                }
            }
            min = timeTaken/60;
            timeTaken = timeTaken%60;
            if (min >9){
                hourMin = ":";
            }
        }
        sec = timeTaken;
        if(sec%60<10){
            minSec = ":0";
        }
        return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
    }
    sec = timeTaken;
    minSec = ":0";
    hourMin = ":0";
    hourHour = "0";
    return formattedTime = (hourHour+hour+hourMin+min+minSec+sec);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:32:42+00:00Added an answer on May 23, 2026 at 8:32 pm

    Using thread you cant update your UI for that you have to use runOnUiThread

    youractivity.this.runOnUiThread(new Runnable(){public void run(){Toast.makeText(mContext, "Message", Toast.LENGTH_LONG).show();}});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class ('Scheduler.as'): package { import flash.events.TimerEvent; import flash.utils.Timer; public class Scheduler
I have a class which looks something like this: class MyClass { public: //
So here's the scoop: I wrote a tiny C# app a while back that
I'm having trouble with the way I designed this little report I'm making. Is
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
I created a splash class with a timer on it, and when the timer
Here is the code I'm using. public class timerApp { Runnable timerRun = new
This is beyond both making sense and my control. That being said here is
I know we are really behind the times here, but we are just about
yesterday you had taught me to query record history within a time range here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.