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

  • Home
  • SEARCH
  • 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 6810061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:09:38+00:00 2026-05-26T20:09:38+00:00

I’ve developed an own implementation of a Chronometer. I did the follow: Create a

  • 0

I’ve developed an own implementation of a Chronometer. I did the follow:

  1. Create a Service (CronoService), that use a Runnable object that execute the thread. The Runnable will loop each 0.1 secs.

  2. Messenger Object that will receive messages from Ui to Start, Pause or Resume the Chronometer.

  3. Intent that will broadcast the time after each loop of the Runnable object.

The code is:

public class CronoService extends Service {

public static final int PARAR = 0;
public static final int EMPEZAR = 1;
public static final int ESTABLECER_TIEMPO = 2;

private static final String TAG = "BroadcastService";

public static final String BROADCAST_ACTION = "jose.planilla.mostrartiempo";
final Messenger mMessenger = new Messenger(new IncomingHandler());

private final Handler handler = new Handler();
private Intent intent;
private long mDec;
private long mTotalMilis;
private long mLastMilis;
private long mElapsedTime;
private long mCurrentMilis;
private int mSeconds;
private int mMin;

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        mCurrentMilis = System.currentTimeMillis();
        mElapsedTime = mCurrentMilis - mLastMilis;
        mLastMilis = mCurrentMilis;
        mTotalMilis += mElapsedTime;
        DisplayLoggingInfo();
        handler.postDelayed(this, 100); // 0.1 seconds
        Log.d("run()", String.valueOf(mTotalMilis));
    }
};  



@Override
public void onCreate(){
    super.onCreate();

    intent = new Intent(BROADCAST_ACTION);

}

@Override
public void onStart(Intent intent, int startId){
    mTotalMilis = intent.getLongExtra("milis", 0);
    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, 100); // 0.1 second 
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStart()");
    mLastMilis = mElapsedTime = System.currentTimeMillis(); 
    mTotalMilis = intent.getLongExtra("milis", 0);
    handler.removeCallbacks(sendUpdatesToUI);
    DisplayLoggingInfo();
    //handler.postDelayed(sendUpdatesToUI, 100); // 0.1 second 

    return START_NOT_STICKY;
}    

@Override
public IBinder onBind(Intent arg0) {
    return mMessenger.getBinder();
}


private void DisplayLoggingInfo() { 
    String tiempo;

    mDec = mTotalMilis % 1000;
    mDec = mDec / 100;
    mSeconds = (int) (mTotalMilis / 1000);
    mMin = mSeconds / 60;
    mSeconds = mSeconds % 60;
    tiempo = "" + mDec;

    if(mSeconds < 10)
        tiempo = ":0" + mSeconds + "." + tiempo;
    else
        tiempo = ":" + mSeconds + "." + tiempo; 

    if(mMin < 10 )
        tiempo = "0" + mMin + tiempo;
    else
        tiempo = mMin + tiempo;

    intent.putExtra("tiempo", tiempo);
    intent.putExtra("milis", mTotalMilis);
    sendBroadcast(intent);
}

@Override
public void onDestroy() {       
    handler.removeCallbacks(sendUpdatesToUI);
    super.onDestroy();
}

public void pause(){
    handler.removeCallbacks(sendUpdatesToUI);
}

public void resume(){
    handler.postDelayed(sendUpdatesToUI, 100);
}

class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg){ 
        switch(msg.what) {
            case PARAR:
                pause();
                break;
            case EMPEZAR:
                resume();
                break;
            case ESTABLECER_TIEMPO: 
                mTotalMilis = (Long) msg.obj;
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

My problem is that the my Chronometer is a bit slower than a normal one. It loses o bit of time en each seconds.
I’m doing something wrong, but I can’t find the problem.
Thanks a lot.

EDIT
Working code updated.

  • 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-26T20:09:38+00:00Added an answer on May 26, 2026 at 8:09 pm

    There’s an easier way that has worked fine for me: playing with Sistem.getCurrentMillis()

    Take a look at this:
    Android: Chronometer with Milliseconds?

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

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.