0% found this document useful (0 votes)
29 views4 pages

Android - Stop Watch Logic - Stack Overflow

hay

Uploaded by

search009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Android - Stop Watch Logic - Stack Overflow

hay

Uploaded by

search009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.

It's100%free,no
registrationrequired.

login

tour

signup

help

stackoverflowcareers

Takethe2minutetour

stopwatchlogic

Iwanttodevelopasimplestopwatchlogicinandroid.
[Link]
codewillbeofgreathelp
android

askedSep17'10at8:46
RahulVarma
1,323

35

74

3Answers

UsetheStopwatchClass(Forhigherprecisionuse [Link]() )
AddaStart()eventandStop()[Link]'llneedtoupdatetheUIsousea
Thread/HandlerCombination.
Thisshouldgetyoustarted.
EDIT:AddedCode.(NiceExercise!:))
Usethe Refresh_Rate toconfigurehowoftenyourUIisupdated.
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
publicclassMainextendsActivityimplementsOnClickListener{
finalintMSG_START_TIMER=0;
finalintMSG_STOP_TIMER=1;
finalintMSG_UPDATE_TIMER=2;
Stopwatchtimer=newStopwatch();
finalintREFRESH_RATE=100;
HandlermHandler=newHandler()
{
@Override
publicvoidhandleMessage(Messagemsg){
[Link](msg);
switch([Link]){
caseMSG_START_TIMER:
[Link]();//starttimer
[Link](MSG_UPDATE_TIMER);
break;
caseMSG_UPDATE_TIMER:
[Link](""+[Link]());
[Link](MSG_UPDATE_TIMER,REFRESH_RATE);//text
viewisupdatedeverysecond,
break;//thoughthetimerisstill
running
caseMSG_STOP_TIMER:
[Link](MSG_UPDATE_TIMER);//nomoreupdates.
[Link]();//stoptimer
[Link](""+[Link]());
break;
default:
break;
}
}
};
TextViewtvTextView;

ButtonbtnStart,btnStop;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
[Link](savedInstanceState);
setContentView([Link]);
tvTextView=(TextView)findViewById([Link].TextView01);
btnStart=(Button)findViewById([Link].Button01);
btnStop=(Button)findViewById([Link].Button02);
[Link](this);
[Link](this);
}
publicvoidonClick(Viewv){
if(btnStart==v)
{
[Link](MSG_START_TIMER);
}else
if(btnStop==v){
[Link](MSG_STOP_TIMER);
}
}
}

editedJul29'14at17:34

answeredSep17'10at9:22
st0le
18k

51

66

Sorrytocontinuewiththequestion...[Link]
[Link],howcaniupdatesayalabelwiththeshowingtime...??? RahulVarma Sep17'10at
9:38
@Rahul,Postwhatyouhave,i'llbuildonthat...st0leSep17'10at9:44
1 [Link]
thetimermuststop...[Link]
time... RahulVarma Sep17'10at10:32
Ofcoursethere'sthestandardChronometerclass:
[Link]/reference/android/widget/Chronometer.htmltbmSep17'14at19:12

[Link]
addafewmethodstoit.
/*
Copyright(c)2005,CoreyGoldberg
[Link];youcanredistributeitand/ormodify
itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
theFreeSoftwareFoundation;eitherversion2oftheLicense,or
(atyouroption)anylaterversion.
*/
[Link];
publicclassStopwatch{
privatelongstartTime=0;
privatebooleanrunning=false;
privatelongcurrentTime=0;
publicvoidstart(){
[Link]=[Link]();
[Link]=true;
}

publicvoidstop(){
[Link]=false;
}
publicvoidpause(){
[Link]=false;
currentTime=[Link]()startTime;
}
publicvoidresume(){
[Link]=true;
[Link]=[Link]()currentTime;
}

//elaspsedtimeinmilliseconds
publiclonggetElapsedTimeMili(){

longelapsed=0;
if(running){
elapsed=(([Link]()startTime)/100)%1000;
}
returnelapsed;
}

//elaspsedtimeinseconds
publiclonggetElapsedTimeSecs(){
longelapsed=0;
if(running){
elapsed=(([Link]()startTime)/1000)%60;
}
returnelapsed;
}
//elaspsedtimeinminutes
publiclonggetElapsedTimeMin(){
longelapsed=0;
if(running){
elapsed=((([Link]()startTime)/1000)/60)%60;
}
returnelapsed;
}
//elaspsedtimeinhours
publiclonggetElapsedTimeHour(){
longelapsed=0;
if(running){
elapsed=(((([Link]()startTime)/1000)/60)/60);
}
returnelapsed;
}
}

Regards
answeredMar12'14at16:22
BilalRabbani
93

Goodexample,justincaseifsomeonewantsthatlayoutfiletogowiththis(prettysimple
though).
<RelativeLayoutxmlns:android="[Link]
xmlns:tools="[Link]
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"/>

editedMar6'14at13:26

answeredMar6'14at13:21
Waqas
1,258

10

You might also like