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 7663345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:02:25+00:00 2026-05-31T14:02:25+00:00

I have developed a Count Down Timer and I am not sure how to

  • 0

I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pause and to resume, click again the timer’s text view.

This is my code:

    Timer = (TextView) this.findViewById(R.id.time); //TIMER  
    Timer.setOnClickListener(TimerClickListener);
    counter = new MyCount(600000, 1000);
}//end of create 

private OnClickListener TimerClickListener = new OnClickListener() {
    public void onClick(View v) {
        updateTimeTask();
    }

    private void updateTimeTask() {
        if (decision == 0) {
            counter.start();
            decision = 1;
        } else if (decision == 2) {
            counter.onResume1();
            decision = 1;
        } else {
            counter.onPause1();
            decision = 2;
        }//end if  
    }

    ;
};

class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }//MyCount  

    public void onResume1() {
        onResume();
    }

    public void onPause1() {
        onPause();
    }

    public void onFinish() {
        Timer.setText("00:00");
        p1++;
        if (p1 <= 4) {
            TextView PScore = (TextView) findViewById(R.id.pscore);
            PScore.setText(p1 + "");
        }//end if  
    }//finish  

    public void onTick(long millisUntilFinished) {
        Integer milisec = new Integer(new Double(millisUntilFinished).intValue());
        Integer cd_secs = milisec / 1000;

        Integer minutes = (cd_secs % 3600) / 60;
        Integer seconds = (cd_secs % 3600) % 60;

        Timer.setText(String.format("%02d", minutes) + ":"
                + String.format("%02d", seconds));
        ///long timeLeft = millisUntilFinished / 1000;  
        /}//on tick  
}//class MyCount  

protected void onResume() {
    super.onResume();
    //handler.removeCallbacks(updateTimeTask);  
    //handler.postDelayed(updateTimeTask, 1000);  
}//onResume  

@Override
protected void onPause() {
    super.onPause();
    //do stuff  
}//onPause  
  • 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-31T14:02:27+00:00Added an answer on May 31, 2026 at 2:02 pm
    /*
     * Copyright (C) 2010 Andrew Gainer
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    // Adapted from Android's CountDownTimer class
    
    package com.cycleindex.multitimer;
    
    import android.os.Handler;
    import android.os.Message;
    import android.os.SystemClock;
    
    /**
     * Schedule a countdown until a time in the future, with
     * regular notifications on intervals along the way.
     *
      * The calls to {@link #onTick(long)} are synchronized to this object so that
     * one call to {@link #onTick(long)} won't ever occur before the previous
     * callback is complete.  This is only relevant when the implementation of
     * {@link #onTick(long)} takes an amount of time to execute that is significant
     * compared to the countdown interval.
     */
    public abstract class CountDownTimerWithPause {
    
        /**
         * Millis since boot when alarm should stop.
         */
      private long mStopTimeInFuture;
    
      /**
       * Real time remaining until timer completes
       */
        private long mMillisInFuture;
    
        /**
         * Total time on timer at start
         */
        private final long mTotalCountdown;
    
        /**
         * The interval in millis that the user receives callbacks
         */
        private final long mCountdownInterval;
    
        /**
         * The time remaining on the timer when it was paused, if it is currently paused; 0 otherwise.
         */
        private long mPauseTimeRemaining;
    
        /**
         * True if timer was started running, false if not.
         */
        private boolean mRunAtStart;
    
        /**
         * @param millisInFuture The number of millis in the future from the call
         *   to {@link #start} until the countdown is done and {@link #onFinish()}
         *   is called
         * @param countDownInterval The interval in millis at which to execute
         *   {@link #onTick(millisUntilFinished)} callbacks
         * @param runAtStart True if timer should start running, false if not
         */
        public CountDownTimerWithPause(long millisOnTimer, long countDownInterval, boolean runAtStart) {
            mMillisInFuture = millisOnTimer;
            mTotalCountdown = mMillisInFuture;
            mCountdownInterval = countDownInterval;
            mRunAtStart = runAtStart;
        }
    
        /**
         * Cancel the countdown and clears all remaining messages
         */
        public final void cancel() {
            mHandler.removeMessages(MSG);
        }
    
        /**
         * Create the timer object.
         */
        public synchronized final CountDownTimerWithPause create() {
            if (mMillisInFuture <= 0) {
                onFinish();
            } else {
              mPauseTimeRemaining = mMillisInFuture;
            }
    
            if (mRunAtStart) {
              resume();
            }
    
            return this;
        }
    
        /**
         * Pauses the counter.
         */
      public void pause () {
        if (isRunning()) {
          mPauseTimeRemaining = timeLeft();
          cancel();
        }
      }
    
      /**
       * Resumes the counter.
       */
      public void resume () {
        if (isPaused()) {
          mMillisInFuture = mPauseTimeRemaining;
          mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
              mHandler.sendMessage(mHandler.obtainMessage(MSG));
          mPauseTimeRemaining = 0;
        }
      }
    
      /**
       * Tests whether the timer is paused.
       * @return true if the timer is currently paused, false otherwise.
       */
      public boolean isPaused () {
        return (mPauseTimeRemaining > 0);
      }
    
      /**
       * Tests whether the timer is running. (Performs logical negation on {@link #isPaused()})
       * @return true if the timer is currently running, false otherwise.
       */
      public boolean isRunning() {
        return (! isPaused());
      }
    
      /**
       * Returns the number of milliseconds remaining until the timer is finished
       * @return number of milliseconds remaining until the timer is finished
       */
      public long timeLeft() {
        long millisUntilFinished;
        if (isPaused()) {
          millisUntilFinished = mPauseTimeRemaining;
        } else {
          millisUntilFinished = mStopTimeInFuture - SystemClock.elapsedRealtime();
          if (millisUntilFinished < 0) millisUntilFinished = 0;
        }
        return millisUntilFinished;
      }
    
      /**
       * Returns the number of milliseconds in total that the timer was set to run
       * @return number of milliseconds timer was set to run
       */
      public long totalCountdown() {
        return mTotalCountdown;
      }
    
      /**
       * Returns the number of milliseconds that have elapsed on the timer.
       * @return the number of milliseconds that have elapsed on the timer.
       */
      public long timePassed() {
        return mTotalCountdown - timeLeft();
      }
    
      /**
       * Returns true if the timer has been started, false otherwise.
       * @return true if the timer has been started, false otherwise.
       */
      public boolean hasBeenStarted() {
        return (mPauseTimeRemaining <= mMillisInFuture);
      }
    
        /**
         * Callback fired on regular interval
         * @param millisUntilFinished The amount of time until finished
         */
        public abstract void onTick(long millisUntilFinished);
    
        /**
         * Callback fired when the time is up.
         */
        public abstract void onFinish();
    
    
        private static final int MSG = 1;
    
    
        // handles counting down
        private Handler mHandler = new Handler() {
    
            @Override
            public void handleMessage(Message msg) {
    
                synchronized (CountDownTimerWithPause.this) {
                    long millisLeft = timeLeft();
    
                    if (millisLeft <= 0) {
                        cancel();
                      onFinish();
                    } else if (millisLeft < mCountdownInterval) {
                        // no tick, just delay until done
                        sendMessageDelayed(obtainMessage(MSG), millisLeft);
                    } else {
                        long lastTickStart = SystemClock.elapsedRealtime();
                        onTick(millisLeft);
    
                        // take into account user's onTick taking time to execute
                        long delay = mCountdownInterval - (SystemClock.elapsedRealtime() - lastTickStart);
    
                        // special case: user's onTick took more than mCountdownInterval to
                        // complete, skip to next interval
                        while (delay < 0) delay += mCountdownInterval;
    
                        sendMessageDelayed(obtainMessage(MSG), delay);
                    }
                }
            }
        };
    }
    

    Source : This Gist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with use count of kernel module being developed.I'd like to
I have a navigation drop-down at the top of a theme I have developed.
I have developed some classes with similar behavior, they all implement the same interface.
I have developed some custom DAO-like classes to meet some very specialized requirements for
We have developed our website(Business users website) in .net Framework 2.0 Our client us
I have developed a VB.NET WCF service that recives and sends back data. When
We have developed a website that uses MVC, C#, and jQuery. In one of
We have developed a webservice that sits and runs in the context of a
I have developed about 300 Applications which I would like to provide with multi-language
I have developed a framework that is used by several teams in our organisation.

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.