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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:28:16+00:00 2026-05-13T20:28:16+00:00

I do have one service running in the background. Whenever it starts I store

  • 0

I do have one service running in the background. Whenever it starts I store in memory the starting time in milliseconds:

startingTime = new Date().getTime();

I want to display a chronometer that starts counting when the service starts and never stops until the user presses a button. I want to allow the user to leave the activity rendering the chronometer, do some stuff and then return. But the idea is that when the user returns I dont want the chronometer to go to 0:00 again. Insted I want it to show the exact time that has passed ever since the service has started.

I can calculate elapsedTime every time the user return to the chronometer activity:

elapsedTime =  new Date().getTime() - startingTime;

The thing is that i dont know how to tell the chronometer to start counting from that time!

Setting it as the chronometer base does not work. Can someon explain what exactly “base” means or how to accomplish this?

thanks a lot!
BYE

  • 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-13T20:28:16+00:00Added an answer on May 13, 2026 at 8:28 pm

    You can use Chronometer.

    You should also check this thread.

    EDIT: The solution:

    public class ChronoExample extends Activity {
     Chronometer mChronometer;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
    
         LinearLayout layout = new LinearLayout(this);
         layout.setOrientation(LinearLayout.VERTICAL);
    
         mChronometer = new Chronometer(this);
    
         // Set the initial value
         mChronometer.setText("00:10");
         layout.addView(mChronometer);
    
         Button startButton = new Button(this);
         startButton.setText("Start");
         startButton.setOnClickListener(mStartListener);
         layout.addView(startButton);
    
         Button stopButton = new Button(this);
         stopButton.setText("Stop");
         stopButton.setOnClickListener(mStopListener);
         layout.addView(stopButton);
    
         Button resetButton = new Button(this);
         resetButton.setText("Reset");
         resetButton.setOnClickListener(mResetListener);
         layout.addView(resetButton);        
    
         setContentView(layout);
     }
    
     private void showElapsedTime() {
         long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();            
         Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis, 
                 Toast.LENGTH_SHORT).show();
     }
    
     View.OnClickListener mStartListener = new OnClickListener() {
         public void onClick(View v) {
          int stoppedMilliseconds = 0;
    
             String chronoText = mChronometer.getText().toString();
             String array[] = chronoText.split(":");
             if (array.length == 2) {
               stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                   + Integer.parseInt(array[1]) * 1000;
             } else if (array.length == 3) {
               stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000 
                   + Integer.parseInt(array[1]) * 60 * 1000
                   + Integer.parseInt(array[2]) * 1000;
             }
    
             mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
             mChronometer.start();
         }
     };
    
     View.OnClickListener mStopListener = new OnClickListener() {
         public void onClick(View v) {
             mChronometer.stop();
             showElapsedTime();
         }
     };
    
     View.OnClickListener mResetListener = new OnClickListener() {
         public void onClick(View v) {
             mChronometer.setBase(SystemClock.elapsedRealtime());
             showElapsedTime();
         }
     };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two windows services running. One service is 'Polling' - it
I have a WCF service (the same one) running on multiple servers, and I'd
I have one windows service which is running in every one min. But I
I have one server and three clients in which a windows service is running
I have created a service running in background which saves gps points on the
This is my first post on StackOverflow! I have a background service running and
I have a service running in background. I started it from an Activity, but
I have developed one application which will have activities and one background service and
This one has us all baffled at work. We have two services running on
I have one test wcf service with default methods and web config is :

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.