Hi i want to make an count down timer and i made one and it work but,
the cout down timer take the System.currentTimeMillis(); .
When i change the time on a device the time on a counter is changed to.
How to make a timer that is not depend on a System.currentTime.
Maybe firebase FieldValue.serverTimestamp()? but how?
And sorry if i post something wrong posting first time.
package com.example.backgroundtimer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final long START_TIME_IN_MILLIS = 3600000 ;
private TextView mTextViewCountDown;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
private long mEndTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewCountDown = findViewById(R.id.text_view_countdown);
mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);
mButtonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mTimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});
mButtonReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resetTimer();
}
});
}
private void startTimer() {
mEndTime = System.currentTimeMillis() + mTimeLeftInMillis;
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
updateButtons();
}
}.start();
mTimerRunning = true;
updateButtons();
}
private void pauseTimer() {
mCountDownTimer.cancel();
mTimerRunning = false;
updateButtons();
}
private void resetTimer() {
mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
updateButtons();
}
private void updateCountDownText() {
int hours = (int) (int) ((mTimeLeftInMillis / (1000*60*60)) % 24);
int minutes = (int) (int) ((mTimeLeftInMillis/ (1000*60)) % 60);
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours,minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
private void updateButtons() {
if (mTimerRunning) {
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setText("Pause");
} else {
mButtonStartPause.setText("Start");
if (mTimeLeftInMillis < 1000) {
mButtonStartPause.setVisibility(View.INVISIBLE);
} else {
mButtonStartPause.setVisibility(View.VISIBLE);
}
if (mTimeLeftInMillis < START_TIME_IN_MILLIS) {
mButtonReset.setVisibility(View.VISIBLE);
} else {
mButtonReset.setVisibility(View.INVISIBLE);
}
}
}
#Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("millisLeft", mTimeLeftInMillis);
editor.putBoolean("timerRunning", mTimerRunning);
editor.putLong("endTime", mEndTime);
editor.apply();
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
}
#Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
mTimeLeftInMillis = prefs.getLong("millisLeft", START_TIME_IN_MILLIS);
Toast.makeText(this,"t "+mTimeLeftInMillis,Toast.LENGTH_LONG).show();
mTimerRunning = prefs.getBoolean("timerRunning", false);
updateCountDownText();
updateButtons();
if (mTimerRunning) {
mEndTime = prefs.getLong("endTime", 0);
mTimeLeftInMillis = mEndTime - System.currentTimeMillis();
if (mTimeLeftInMillis < 0) {
mTimeLeftInMillis = 0;
mTimerRunning = false;
updateCountDownText();
updateButtons();
} else {
startTimer();
}
}
}
}
Related
I have a problem with a small counter system that I made in Android Studio the problem is that I don't know how to do the counter plus 1.5 because now it is all the time when I press the plus button it does all the time 1 there but that must be 1.5 in the script below geld++; ensures that 1 is added all the time when you press the button that just works well but I don't want 1 but I want 1.5 all the time but I can't find a solution anywhere. So I hope someone can help. Thanks in advance!
package com.example.melkanalysetimer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class wit extends AppCompatActivity {
private TextView countdownText;
private Button countdownButton;
private Button krat;
private Button remove;
private TextView scoreb;
private TextView money;
private TextView reset;
private CountDownTimer countDownTimer;
private long timeLeftInMilliseconds = 960000;
private boolean timerRunning;
private static final long START_TIME_IN_MILLIS = 960000;
int score = 0;
int geld = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wit);
countdownText = findViewById(R.id.countdown_text);
countdownButton = findViewById(R.id.countdown_button);
krat = findViewById(R.id.b_add);
scoreb = findViewById(R.id.tv_score);
remove = findViewById(R.id.b_remove);
money = findViewById(R.id.ssgeld);
reset = findViewById(R.id.button_reset);
scoreb.setText("Kratjes: "+ score);
money.setText("Geld: "+ geld);
krat.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
score++;
scoreb.setText("Kratjes: "+ score);
geld++;
money.setText("Geld: "+ geld);
}
});
remove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
score--;
scoreb.setText("Kratjes: "+ score);
geld--;
money.setText("Geld: "+ geld);
}
});
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
resetTimer();
}
});
countdownButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startStop();
}
});
updateTimer();
}
public void startStop() {
if (timerRunning) {
stopTimer();
} else {
startTimer();
}
}
public void startTimer() {
countDownTimer = new CountDownTimer(timeLeftInMilliseconds, 1000) {
#Override
public void onTick(long l) {
timeLeftInMilliseconds = l;
updateTimer();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUZE");
timerRunning = true;
}
public void stopTimer() {
countDownTimer.cancel();
countdownButton.setText("START");
timerRunning = false;
}
public void resetTimer() {
timeLeftInMilliseconds = START_TIME_IN_MILLIS;
updateTimer();
}
public void updateTimer() {
int minutes = (int) timeLeftInMilliseconds / 60000;
int seconds = (int) timeLeftInMilliseconds % 60000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if (seconds < 10) timeLeftText += "0";
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
}
First you need to change Int to Double like
double score = 0.00;
Then Increment/Decrement like this
Decrement
remove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
//check if score is less then 0
if(score > 1.5){
score = score - 1.5;
}
scoreb.setText("Kratjes: "+ score);
geld--;
money.setText("Geld: "+ geld);
}
});
Increment
krat.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
score = score + 1.5;
scoreb.setText("Kratjes: "+ score);
geld++;
money.setText("Geld: "+ geld);
}
});
i an developing a simple task in which i want to make a countdown timer running still in background when my app is stopped and closed. and all this work should b done without using android services...
Here is the code what i am using. in this code time keeps running in background when i Minimizes my app not close... but when i close my app then it starts from beginning which is not suitable for me... what i want that the time keeps on running until i close my app.
public class MainActivity extends AppCompatActivity {
private static long START_TIME_IN_MILLIS = 90000;
private TextView mTextViewCountDown;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis;
private long mEndTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewCountDown = findViewById(R.id.text_view_countdown);
mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);
startTimer();
}
private void startTimer() {
mEndTime = System.currentTimeMillis() + START_TIME_IN_MILLIS;
mCountDownTimer = new CountDownTimer(START_TIME_IN_MILLIS, 1000) {
#Override
public void onTick(long millisUntilFinished) {
START_TIME_IN_MILLIS = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
}
}.start();
mTimerRunning = true;
}
private void updateCountDownText() {
int minutes = (int) (START_TIME_IN_MILLIS / 1000) / 60;
int seconds = (int) (START_TIME_IN_MILLIS / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
#Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("millisLeft", START_TIME_IN_MILLIS);
editor.putBoolean("timerRunning", mTimerRunning);
editor.putLong("endTime", mEndTime);
editor.apply();
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
}
#Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
START_TIME_IN_MILLIS = prefs.getLong("millisLeft", START_TIME_IN_MILLIS);
mTimerRunning = prefs.getBoolean("timerRunning", false);
updateCountDownText();
mEndTime = prefs.getLong("endTime", mEndTime);
START_TIME_IN_MILLIS = mEndTime - System.currentTimeMillis();
Comm.endTime = (int) START_TIME_IN_MILLIS;
startTimer();
}
}
I'm trying to loop my countdown timer for a specific number of times, but I'm not sure where I should add my for loop... Currently trying to do an interval timer function (I know there's a way to do it via Handler, but I'm still a beginner and got kind of confused on how I shoul use the handler)
I've tried adding it at '.start' and the StartTimer function but the countdown time still stays at 0. Would be great if any assistance is given as I'm still a beginner.. Thanks!
package com.example.bushykai.myapplication;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Timer extends AppCompatActivity {
private static final long START_TIME_IN_MILLIS=10000;
private int sets = 3;
private TextView textViewCountdown;
private Button buttonStartPause;
private Button buttonReset;
private CountDownTimer countDownTimer;
private boolean timerRunning;
private long timeLeftInMillis = START_TIME_IN_MILLIS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
textViewCountdown = findViewById(R.id.countdownTimer);
buttonStartPause = findViewById(R.id.startPause);
buttonReset= findViewById(R.id.reset);
buttonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (timerRunning) {
pauseTimer();
}
else {
startTimer();
}
}
});
buttonReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resetTimer();
}
});
updateCountDownText();
}
private void pauseTimer() {
countDownTimer.cancel();
timerRunning = false;
buttonStartPause.setText("Start");
buttonReset.setVisibility(View.VISIBLE);
}
private void startTimer() {
countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
timerRunning = false;
buttonStartPause.setText("Start");
buttonStartPause.setVisibility(View.INVISIBLE);
buttonReset.setVisibility(View.VISIBLE);
}
}.start();
timerRunning = true;
buttonStartPause.setText("Pause");
buttonReset.setVisibility((View.INVISIBLE));
}
private void resetTimer() {
timeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
buttonReset.setVisibility(View.INVISIBLE);
buttonStartPause.setVisibility(View.VISIBLE);
}
private void updateCountDownText() {
int minutes = (int) (timeLeftInMillis / 1000 ) / 60;
int seconds = (int) (timeLeftInMillis / 1000 ) % 60;
String timeLeftFormatted = String.format("%02d:%02d", minutes, seconds);
textViewCountdown.setText(timeLeftFormatted);
}
}
You have to add a Runnable Handler to your class try code below and modify with your Countdown Timer
public Handler timerHandler = new Handler();
public Runnable timerRunnable = new Runnable() {
#Override
public void run() {
//show alert or DO Whatever
}
};
public void timerStart() {
timerStop();
timerHandler.postDelayed(timerRunnable, Constants.TIMER_TIME_OUT);
}
public void timerStop() {
timerHandler.removeCallbacks(timerRunnable);
}
I want to ask a question I have put the timer to count down from 10:00 min..I want it when it goes at 2 min to put a sound for 10 seconds like an error sound or alarm sound is that possible and if it is can I have some help plz??
Yes it's possible, all you have to do to check the remaining time and play the sound accordingly:
private static final int Two_Seconds = 120000;
public void startTimer(){
countDownTimer = new CountDownTimer(timeLeftInMilliseconds,10000) {
#Override
public void onTick(long millisUntilFinished) {
if(millisUntilFinished <= Two_Seconds){
// display your sound
}
timeLeftInMilliseconds = millisUntilFinished;
updateTimer();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUSE");
timerRunning = true;
}
countdownText = findViewById(R.id.countdown_text);
countdownButton = findViewById(R.id.countdown_button);
countdownButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startstop();
}
});
updateTimer();
}
public void startstop(){
if(timerRunning){
stopTimer();
}else {
startTimer();
}
}
public void startTimer(){
countDownTimer = new CountDownTimer(timeLeftInMilliseconds,1000) {
#Override
public void onTick(long millisUntilFinished) {
timeLeftInMilliseconds = millisUntilFinished;
updateTimer();
}
#Override
public void onFinish() {
}
}.start();
countdownButton.setText("PAUSE");
timerRunning = true;
}
public void stopTimer (){
countDownTimer.cancel();
countdownButton.setText("START");
timerRunning = false;
}
public void updateTimer (){
int minutes = (int) timeLeftInMilliseconds / 60000;
int seconds = (int) timeLeftInMilliseconds % 60000 / 1000;
String timeLeftText;
timeLeftText = "" + minutes;
timeLeftText += ":";
if(seconds <7)timeLeftText +=0;
timeLeftText += seconds;
countdownText.setText(timeLeftText);
}
}
I Want to create a timer which will run for 10 seconds. If the user have entered the correct answer the timer should restart. For that I have added cancel.countdowntimer(). But it's not working. The app crashes. Where should I use that countdowntimer.cancel() function? Should I use a method for countdowntimer.cancel()?
package com.example.vignesh.work;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button b1;
int loc;
int total=0;
int mark=0;
Runnable run;
Handler handler;
CountDownTimer countDownTimer;
TextView textView4,textView2,textView1,textView3;
Button b11,b22,b33,b44;
Random random = new Random();
ArrayList<Integer> answers = new ArrayList<Integer>();
public void nextQuestion() {
new CountDownTimer(10000 + 100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
textView3.setText(Long.toString(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
textView3.setText("10");
}
}.start();
int a = random.nextInt(20) + 1;
int b = random.nextInt(20) + 1;
textView1.setText(Integer.toString(a) + "+" + Integer.toString(b));
loc = random.nextInt(4);
answers.clear();
for (int i = 0; i < 4; i++) {
if (i == loc) {
answers.add(a + b);
} else {
answers.add((a + b) + random.nextInt(10) + 1);
}
}
b11.setText(Integer.toString(answers.get(0)));
b22.setText(Integer.toString(answers.get(1)));
b33.setText(Integer.toString(answers.get(2)));
b44.setText(Integer.toString(answers.get(3)));
}
public void choose(View view)
{
if(view.getTag().toString().equals(Integer.toString(loc+1)))
{
textView4.setText("Correct");
mark++;
}
else
{
textView4.setText("Wrong");
}
total++;
try {
countDownTimer.cancel();
}
catch (Exception e)
{
Log.i("error",e.getMessage());
}
nextQuestion();
textView2.setText(Integer.toString(mark)+"/"+Integer.toString(total));
}
public void click(View view)
{
b1.setVisibility(view.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.buttongo);
b11 = (Button)findViewById(R.id.optiona);
b22 = (Button)findViewById(R.id.optionb);
b33 = (Button)findViewById(R.id.option3);
b44 = (Button)findViewById(R.id.option4);
textView1=(TextView)findViewById(R.id.textView1);
textView2=(TextView)findViewById(R.id.textView2);
textView3=(TextView)findViewById(R.id.textView3);
textView4=(TextView)findViewById(R.id.textView4);
nextQuestion();
}
}
**AFTER ADDING EXCEPTIONAL HANDLING THE ERROR WAS:Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference**
Thanks.
You instantiate your CountDownTimer in nextQuestion() method, however you do not assign it in the countDownTimer variable defined in the top of your class: CountDownTimer countDownTimer;
Change the code inside nextQuestion() method to:
countDownTimer = new CountDownTimer(10000 + 100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
textView3.setText(Long.toString(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
textView3.setText("10");
}
};
countDownTimer.start();
Note that I am assigning the new CountDownTimer to countDownTimer variable and then start the timer by calling countDownTimer.start();.
Then check if countDownTimer is null before calling it to cancel:
if(countDownTimer != null) {
countDownTimer.cancel();
}
You are not initialising countDownTimer variable update your method like below and also check null before calling countDownTimer.cancel();
public void nextQuestion() {
countDownTimer = new CountDownTimer(10000 + 100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
textView3.setText(Long.toString(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
textView3.setText("10");
}
}.start();
int a = random.nextInt(20) + 1;
int b = random.nextInt(20) + 1;
textView1.setText(Integer.toString(a) + "+" + Integer.toString(b));
loc = random.nextInt(4);
answers.clear();
for (int i = 0; i < 4; i++) {
if (i == loc) {
answers.add(a + b);
} else {
answers.add((a + b) + random.nextInt(10) + 1);
}
}
b11.setText(Integer.toString(answers.get(0)));
b22.setText(Integer.toString(answers.get(1)));
b33.setText(Integer.toString(answers.get(2)));
b44.setText(Integer.toString(answers.get(3)));
}