ANDROID - stop watch logic

I want to develop a simple stop watch logic in android.

On clicking a list view the timer should start and on clicking the button the timer should stop. Can anyone please guide me. Any sample code will be of great help

This question and answers originated from www.stackoverflow.com
Question by (9/17/2010 8:46:52 AM)

Answer

Use the Stopwatch Class (For higher precision use System.nanoTime())

Add a Start() event and Stop() event on Button Presses. You'll need to update the UI so use a Thread/Handler Combination.

This should get you started.

EDIT: Added Code. (Nice Exercise! :) )

Use the Refresh_Rate to configure how often your UI is updated.

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener{

    final int MSG_START_TIMER = 0;
    final int MSG_STOP_TIMER = 1;
    final int MSG_UPDATE_TIMER = 2;

    Stopwatch timer = new Stopwatch();
    final int REFRESH_RATE = 100;

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case MSG_START_TIMER:
                timer.start(); //start timer
                mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
                break;

            case MSG_UPDATE_TIMER:
                tvTextView.setText(""+ timer.getElapsedTime());
                mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
                break;                                  //though the timer is still running
            case MSG_STOP_TIMER:
                mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.
                timer.stop();//stop timer
                tvTextView.setText(""+ timer.getElapsedTime());
                break;

            default:
                break;
            }
        }
    };

    TextView tvTextView;
    Button btnStart,btnStop;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvTextView = (TextView)findViewById(R.id.TextView01);

        btnStart = (Button)findViewById(R.id.Button01);
        btnStop= (Button)findViewById(R.id.Button02);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);

    }

    public void onClick(View v) {
        if(btnStart == v)
        {
            mHandler.sendEmptyMessage(MSG_START_TIMER);
        }else
        if(btnStop == v){
            mHandler.sendEmptyMessage(MSG_STOP_TIMER);
        }
    }
}
Answer by

Find More Answers
Related Topics  android
Related Questions
  • stop watch in android

    i need to create stopwatch with start,stop and pause in android. please assist me. not yet started any coding.
  • Stop watch app inventor

    I'm new user of App inventor. I need small stopwatch. Basic function: - set time ex. 25 min (if time=0;alarm=true) - start time - reset time (if you want reset time you will see the comfir box (y…
  • stop watch problem in android

    i creating for stopwatch with start,stop i use following code it started but not stop. please help me. my code: public class StopWatch2 extends Activity implements Runnable{ // text view influen…
  • Stop watch app using service and BroadcastReceiver

    I am working on a program that is kind of like a stop watch, that notifies a user to different things. They need to be able to use other apps while waiting for the notification. The notification wil…
  • How can I make stop watch in Android?

    I am need stop watch like widget in android.Currently I am using Android chronometer,it is not fulfilling my requirements as when I am stopping the chronometer its stops in UI but but it continues t…
  • Android debugging - Watch points

    I am trying to use watchpoints to debug an Android problem in Eclipse. I'm settings a watchpoint by settings a breakpoint on my variable definition. However the execution never pauses even though th…
  • Android : Watch video

    How can I put in my activity a video from URL ? such ad new android market. It's possibile ?
  • Activity Navigation Logic

    I have 3 Activities...the first Activity have a button that start third Activity. The problem is: When I am in 3 - rd activity press "back" button its navigate me back to 1 - st Activity, but i n…
  • Stop uninstallation of application

    i create an app and install it on phone. now i want to add a feature that my app should not uninstall from the phone. so i think if one of the following issues could be solved is there anyway t…
  • stop SMS propagation

    I am trying to not to propagate an sms upon receiving like this public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { abortB…