Stopwatch repeating time one after another with pause between - java

this is my first time writing here. I am learning Android development and trying to make a stopwatch (countdown timer) app, which has 12 rounds and pause - example 10 sec between them. I wrote a code which works fine but I want to write it more "readable and nicely" so I would like to ask you for your help. As you can see, I don't want to write every time under public void run() in if-else if statement new roundtime number and setting text because it would take too much space. That is why I wrote it just for example(not 12 times) so you can see what I mean. Do you have any suggestions how to write it better for 12 rounds? Can I use a loop? If u have easier way for countdown round after round I would appreciate it. I am sorry for my english. Any comments are welcome. Enjoy coding.
This is my code:
public class Main3Activity extends AppCompatActivity {
Handler handler;
TextView textView;
ImageView imageView2;
ImageView imageView3;
Boolean checker=false;
long secondtime=1000;
long roundtime1= 10000;
long roundtime2=10000;
long pause= 31000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView=(TextView)findViewById(R.id.textView3);
imageView2=(ImageView)findViewById(R.id.imageView2);
imageView3=(ImageView)findViewById(R.id.imageView3);
imageView2.setOnClickListener(
new ImageView.OnClickListener(){
#Override
public void onClick(View v) {
handler = new Handler();
if (checker == false) {
handler.post(r);
checker = true;
}
imageView3.setOnClickListener(
new ImageView.OnClickListener() {
#Override
public void onClick(View v) {
handler.removeCallbacks(r);
}
}
);
}
final Runnable r= new Runnable() {
#Override
public void run() {
roundtime = roundtime - secondtime;
int seconds = (int) (roundtime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (roundtime >= 0) {
textView.setText(String.format("%d:%02d", minutes, seconds));
handler.postDelayed(r, 1000);
} else if (pause > 0) {
pause = pause - secondtime;
int secondss = (int) (pause / 1000);
int minutess = secondss / 60;
secondss = secondss % 60;
textView.setText(String.format("%d:%02d", minutess, secondss));
handler.postDelayed(r, 1000);
} else if (roundtime2 > 0) {
roundtime2 = roundtime2 - secondtime;
int secondsss = (int) (roundtime2 / 1000);
int minutesss = secondsss / 60;
secondsss = secondsss % 60;
textView.setText(String.format("%d:%02d", minutesss, secondsss));
handler.postDelayed(r, 1000);
}else{
textView.setText("Over");
}
}
};
}
);
}
}

Related

How to kill reverseTimer at this

Can you tell me how to kill this Timer, so after that i can create new timer again
public void reverseTimer(int seconds, final TextView tv)
{
new CountDownTimer(seconds * 1000 + 1000,1000)
{
public void onTick(long millisUntilFinished){
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tv.setText(String.format("%02d",minutes)
+ ":" + String.format("%02d",seconds));
}
public void onFinish(){
tv.setText("Completed");
Intent myintent = new Intent(QuestionActivity.this,ResultActivity.class);
myintent.putExtra("total",String.valueOf(total));
myintent.putExtra("correct",String.valueOf(correct));
myintent.putExtra("incorrect",String.valueOf(wrong));
startActivity(myintent);
}
}.start();
how to create kill/cancel function at this
Welcome to SO community.
You can create a field for the CountDownTimer, and invoke .cancel() to get it over & renew it again by using your method reverseTimer().
and use a boolean to track whether the quiz question is ansewred by the user or not; When they answer it, then call restartTimer() to go to the next question
private CountDownTimer mTimer;
private boolean mIsAnswered = false;
public void reverseTimer(int seconds, final TextView tv)
{
if (mIsAnswered) {
mIsAnswered = false;
nextQuestion();
return;
}
mTimer = new CountDownTimer(seconds * 1000 + 1000,1000)
{
public void onTick(long millisUntilFinished){
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tv.setText(String.format("%02d",minutes)
+ ":" + String.format("%02d",seconds));
}
public void onFinish(){
nextQuestion();
}
}.start();
}
public void restartTimer() {
if (mTimer != null)
mTimer.cancel();
mIsAnswered = true;
restartTimer(...);
}
public void nextQuestion() {
tv.setText("Completed");
Intent myintent = new Intent(QuestionActivity.this,ResultActivity.class);
myintent.putExtra("total",String.valueOf(total));
myintent.putExtra("correct",String.valueOf(correct));
myintent.putExtra("incorrect",String.valueOf(wrong));
startActivity(myintent);
}
private CountDownTimer timer;
timer = new CountDownTimer(seconds * 1000 + 1000,1000){
public void onTick(long millisUntilFinished){
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tv.setText(String.format("%02d",minutes)
+ ":" + String.format("%02d",seconds));
}
public void onFinish(){
tv.setText("Completed");
Intent myintent = new Intent(QuestionActivity.this,ResultActivity.class);
myintent.putExtra("total",String.valueOf(total));
myintent.putExtra("correct",String.valueOf(correct));
myintent.putExtra("incorrect",String.valueOf(wrong));
startActivity(myintent);
}
};
timer.start();
private void cancelTimer(){
if(timer !=null){
timer.cancel();
}
}

TextView stops updating after a while using hourglass tick function

// After running for a few minutes the UI stop updating inside of the tick function.
//This is update function of my text view in given below
private void timerGreen(final long time, long interval) {
hourglassGreen = new Hourglass(time, interval) {
#Override
public void onTimerTick(long timeRemaining) {
updateUIGreen(timeRemaining);
if (soundrunning) {
soundTick = new SoundTick();
soundTick.playSound(MainActivity.this);
}
}
#Override
public void onTimerFinish() {
}
};
}
// here it is my textview call in my function
private void updateUIGreen(long timeRemain) {
greenTextView.setText(correctFormat(timeRemain));
}
//here my correctFormat method
public String correctFormat(long millisUntilFinished) {
int minutes = (int) (millisUntilFinished / 1000) / 60;
int secs = (int) (millisUntilFinished / 1000) % 60;
return String.format(Locale.getDefault(), "%02d:%02d", minutes, secs);
}

How to make a timer which executes for certain amount of time

I'm in the middle of a Football Match Timer project. Basically, I'm looking for an implementation of a timer which starts after pushing a "START 1st half" button, counts to 45 minutes, than pauses and we are able to start it again pushing "START 2nd half"(it would be the same button, but its text would be changed through the whole match). Then it would count from 45 minutes to 90 minutes.
I've been trying to accomplish this using Handler(), Runnable() and onClickListener(), but it doesn't work at all for me :( Would you give me some suggestions about how to tackle this?
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
showTimeRemaining();
timerHandler.postDelayed(this, 500);
}
};
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
startButton(v);
if (b.getText().equals("Mecz trwa")) {
timerHandler.removeCallbacks(timerRunnable);
b.setEnabled(true);
} else {
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
b.setText("Mecz trwa");
b.setEnabled(false);
}
}
});
public void showTimeRemaining() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
timerTextView = (TextView) findViewById(R.id.time);
timerTextView.setText(String.format("%d:%02d", minutes, seconds));
}
Here are some of my suggestions.
First, encapsulate your Handler into a class called Timer. This way it's easier to manipulate your timers. Here is my version:
import android.os.Handler;
public class Timer {
private Handler handler;
private boolean paused;
private int interval;
private Runnable task = new Runnable () {
#Override
public void run() {
if (!paused) {
runnable.run ();
Timer.this.handler.postDelayed (this, interval);
}
}
};
private Runnable runnable;
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
public void startTimer () {
paused = false;
handler.postDelayed (task, interval);
}
public void stopTimer () {
paused = true;
}
public Timer (Runnable runnable, int interval, boolean started) {
handler = new Handler ();
this.runnable = runnable;
this.interval = interval;
if (started)
startTimer ();
}
}
Secondly, don't use System.currenTimeMillis. Use something more manipulatable. Create a variable of your own that stores how many seconds are left:
private int secondsLeft = 60 * 45;
You decrement this variable every second, until it reaches zero. Then, you stop the timer, change the button's text or whatever. This logic should be put into the Runnable used for the handler.
I would suggest to try using of CountDownTimer.
Refer here for documentation and usage:
https://developer.android.com/reference/android/os/CountDownTimer.html

Timer code crashes my app

I'm learning android development, and what I'm trying to do is to have a label that counts down from 40 minutes, and when it reaches 0 it would stop counting and do something else. This is my code:
#Override
protected void onStart() {
super.onStart();
count = 2400;
final Timer t = new Timer();//Create the object
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
minLeft = (int) Math.floor(count / 60);
secLeft = count - minLeft * 60;
counter = minLeft + ":" + secLeft;
TextView tv = (TextView) findViewById(R.id.timer);
Log.i(MainActivity.TAG,minLeft+", "+secLeft+", "+counter);
tv.setText(counter);
count--;
if (minLeft <= 0 && secLeft <= 0) {
t.cancel();
count = 2400;
onFinish();
}
}
}, 1000, 1000);
}
But, when I go to that activity by clicking a button in the main activity, the label has the text "Timer" (its original text), and after a few seconds the app crashes with CalledFromWrongThreadException, but the line that causes the problem seems to be the one where I set the text of the TextView.
Please help, thanks in advance.
Your scheduled task runs on the background thread.
And you try to set the text to the textview from this background thread.
However in Android all view related operations have to be done on the main thread.
That is way in your scheduled task you have to use something like:
#Override
protected void onStart() {
super.onStart();
count = 2400;
final Timer t = new Timer();//Create the object
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
minLeft = (int) Math.floor(count / 60);
secLeft = count - minLeft * 60;
counter = minLeft + ":" + secLeft;
// the name of your actual activity
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
TextView tv = (TextView) findViewById(R.id.timer);
Log.i(MainActivity.TAG,minLeft+", "+secLeft+", "+counter);
tv.setText(counter);
}
});
count--;
if (minLeft <= 0 && secLeft <= 0) {
t.cancel();
count = 2400;
onFinish();
}
}
}, 1000, 1000);
}
Please also note, that this code can be written more elegantly, without all/so many anonymous classes, but it should do the trick.

Android: Count Down Timer e.g. 10:00 to 00:00? using OnclickListener to TextView?

I'm trying to make a countdown timer starting at 10 minutes, similair to a basketball scoreboard: 10:00 to 00:00. How would I do that? This is my code:
private TextView Timer;
Handler handler = new Handler();
private int length = 120000;
private int decision = 0;
MyCount counter;
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min= "0" + minutes;
output = min + " : " + sec;
return output;
}//formatTime
#Override
public void onCreate(Bundle cute) {
super.onCreate(cute);
counter = new MyCount(length, 1000);
updateTime();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}//end of cuteness
private Runnable updateTimeTask = new Runnable() {
public void run() {
updateTime();
handler.postDelayed(this, 1000);
}
};
private void updateTime() {
switch (decision) {
case 0:
startTime = 0L;
counter.start();
decision=1;
break;
case 1:
counter.onPause();
decision=0;
break;
}
}//updateTime
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onPause() {
//do stuff later
onPause();
}//finish
public void onTick(long millisUntilFinished) {
Timer.setText("" + formatTime(millisUntilFinished));
}//on tick
#Override
public void onFinish() {
onStop();
}//finish
}//class MyCount
Any help would be appreciated. thanks!
This does not have to be too hard. You've already created your functionality for writing your time in letters, now you need to count down. Starting a timer is easy, just do this in your start button event handler (or whatever you choose to use) (modified example from the android developer reference:
// New timer for 10 minutes, starts after initialization
new MyCount(600000, 1000)
{
// Updates the text on your "scoreboard" every second
public void onTick(long millisUntilFinished)
{
Timer.setText("Time remaining: " + formatTime(millisUntilFinished));
}
public void onFinish()
{
mTextField.setText("done!");
}
}.start();
And that's all you need! You can skip your UpdateTime function and your updateTimeTask. Just replace all this on your onCreate method
counter = new MyCount(length, 1000);
updateTime();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
With my code. Or modify it as you please!
why don't you just create 3 textviews so that it will be easier for you to code?
one for the minutes.
one for the colon.
one for the seconds.
then use the code he's using.
hope I helped.
The way i think of making a count down is just saving at the beginning the (current time + 10 minutes) aside and then just subtract it from current time every second in your handler and display the result in the desired format..

Categories

Resources