Timer setts of and stops when an activity has been fulfilled

Hello Everyone,

I am still building my application and I have developed an alarm. What I need next is a timer that starts off when the alarm rings and registers the amount of time it takes the user to stop the alarm from ringing.

This is the Java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivityextends Activity {

TextViewtimerTextView;
long startTime =0;

//runs without a timer by reposting this handler at the end of the runnable  
HandlertimerHandler =new Handler();
RunnabletimerRunnable =new Runnable() {

@Override  
public void run() {
long millis = System.currentTimeMillis() -startTime;
int seconds = (int) (millis /1000);
int minutes = seconds /60;
seconds = seconds %60;

timerTextView.setText(String.format("%d:%02d", minutes, seconds));

timerHandler.postDelayed(this,500);
}
};

@Override  
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_test);

timerTextView = (TextView) findViewById(R.id.timerTextView);

Button b = (Button) findViewById(R.id.button);
b.setText("start");
b.setOnClickListener(new View.OnClickListener() {

@Override  
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("stop")) {
timerHandler.removeCallbacks(timerRunnable);
b.setText("start");
}else {
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable,0);
b.setText("stop");
}
}
});
}

@Override  
public void onPause() {
super.onPause();
timerHandler.removeCallbacks(timerRunnable);
Button b = (Button)findViewById(R.id.button);
b.setText("start");
}

}

This is the xml:

\<?xml version="1.0" encoding="utf-8"?\>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:app="http://schemas.android.com/apk/res-auto"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout\_width="match\_parent"  
android:layout\_height="match\_parent"  
tools:context=".TestActivity">

<TextView  
android:id="@+id/timerTextView"  
android:layout\_width="wrap\_content"  
android:layout\_height="wrap\_content"  
android:text="Hello World!"  
app:layout\_constraintBottom\_toBottomOf="parent"  
app:layout\_constraintLeft\_toLeftOf="parent"  
app:layout\_constraintRight\_toRightOf="parent"  
app:layout\_constraintTop\_toTopOf="parent" />

<Button  
android:id="@+id/button"  
android:layout\_width="wrap\_content"  
android:layout\_height="wrap\_content"  
android:layout\_marginBottom="110dp"  
android:text="Button"  
app:layout\_constraintBottom\_toBottomOf="parent"  
app:layout\_constraintEnd\_toEndOf="parent"  
app:layout\_constraintStart\_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

This is my code. I do not need the button and the timer must start and stop when certain conditions have been met. In my case, it is when the user stopped the alarm. The time it took the user to stop the alarm should also apear on the result page. Could anyone offer some source code for that please? Thank you! :) 

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.

As you’ll notice, your Topic is now in the Project Development Help and Advice board. No action is needed on your part; you can continue the conversation as normal here.