Click here to load reader
Upload
search009
View
221
Download
0
Embed Size (px)
DESCRIPTION
hay
Citation preview
sign up log in tour help stack overflow careers
Take the 2minute tour ×Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.
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 samplecode will be of great help
android
asked Sep 17 '10 at 8:46Rahul Varma1,323 8 35 74
3 Answers
Use the Class (For higher precision use )Stopwatch System.nanoTime()
Add a Start() event and Stop() event on Button Presses. You'll need to update the UI so use aThread/Handler Combination.
This should get you started.
EDIT: Added Code. (Nice Exercise! :) )
Use the to configure how often your UI is updated.Refresh_Rate
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); } }}
edited Jul 29 '14 at 17:34 answered Sep 17 '10 at 9:22st0le18k 5 51 66
–
Sorry to continue with the question...Actually i am stuck at the updating the UI in android. I have used thisclass in java. But, how can i update say a label with the showing time...??? Rahul Varma Sep 17 '10 at9:38
– @Rahul, Post what you have, i'll build on that... st0le Sep 17 '10 at 9:44
1
–
I have a simple layout. A label and two buttons. On clicking start a timer must start and on clicking stopthe timer must stop... Thats all. Nothing complex. Just need how to run timer and updating the label withtime... Rahul Varma Sep 17 '10 at 10:32
–
Of course there's the standard Chronometer class:developer.android.com/reference/android/widget/Chronometer.html tbm Sep 17 '14 at 19:12
As Mr gave an excellent example by using class. I modified this class a little andadd a few methods to it.
st0le Stopwatch
/*Copyright (c) 2005, Corey Goldberg
StopWatch.java is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.*/
package bilalrabbani1.at.live.com;
public class Stopwatch {private long startTime = 0;private boolean running = false;private long currentTime = 0;
public void start() { this.startTime = System.currentTimeMillis(); this.running = true;}
public void stop() { this.running = false;}
public void pause() { this.running = false; currentTime = System.currentTimeMillis() ‐ startTime;}public void resume() { this.running = true; this.startTime = System.currentTimeMillis() ‐ currentTime;}
//elaspsed time in millisecondspublic long getElapsedTimeMili() {
long elapsed = 0; if (running) { elapsed =((System.currentTimeMillis() ‐ startTime)/100) % 1000 ; } return elapsed;}
//elaspsed time in secondspublic long getElapsedTimeSecs() { long elapsed = 0; if (running) { elapsed = ((System.currentTimeMillis() ‐ startTime) / 1000) % 60; } return elapsed;}
//elaspsed time in minutespublic long getElapsedTimeMin() { long elapsed = 0; if (running) { elapsed = (((System.currentTimeMillis() ‐ startTime) / 1000) / 60 ) % 60; } return elapsed;}
//elaspsed time in hourspublic long getElapsedTimeHour() { long elapsed = 0; if (running) { elapsed = ((((System.currentTimeMillis() ‐ startTime) / 1000) / 60 ) / 60); } return elapsed;}}
Regards
answered Mar 12 '14 at 16:22Bilal Rabbani93 1 9
Good example, just in case if someone wants that layout file to go with this (pretty simplethough).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" >
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
<EditText android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginLeft="18dp" android:layout_marginTop="49dp" android:ems="10" >
<requestFocus /></EditText>
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/TextView01" android:layout_marginTop="42dp" android:layout_toRightOf="@+id/textView1" android:text="Start" />
<Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Button01" android:layout_marginTop="14dp" android:layout_toRightOf="@+id/textView1" android:text="Stop" />
edited Mar 6 '14 at 13:26 answered Mar 6 '14 at 13:21Waqas1,258 9 10