how to stop scheduled timer after dialog is closed - java

I have a scheduled timer running to show the delay in coming to school. Whenever a student arrives to the school, a custom dialog opens up with display showing the delay in arrival : 20.0 min. It gets incremented by 0.5min every half a minute. My code is -
public void startTimer(long delay_minutes) {
final long delay = delay_minutes;
delay_countup = (double) delay;
//Start the scheduled time
departuretimer = new Timer();
departuretimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
countup = 0.0 + delay_countup;
Log.d("hi","Values 0" + delay_countup + countup);
mHandler.obtainMessage(1).sendToTarget();
delay_countup = delay_countup + 0.5;
Log.d("hi","Values 1" + delay_countup);
}
}, 0, 30000);
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
delay_time.setText(String.valueOf(countup) + "min");
rootView.invalidate();
}
};
The problems are -
a) the timer runs in background for the old arrived student even when the dialog is opened for new arrived student. I mean the timer is never killed when the dialog is closed.(The dialog is closed only to confirm the arrival of the student)
b) Sometimes the textview delay_timedisplays wrong value. It shows 22.0min and immediately 0.5min and then again 23.0min.
Why is this?
EDIT 1:
Handling timer cancel after click of button in the dialog
private void handleClickAction() {
dismiss();
timer.cancel();
timer = null;
}
EDIT 2 :
The logs always display correct values but in the UI sometimes there is a problem. The problem is that for example -
delay_countup = 50.0
countup = 50.0
Textview updates as 50.0 //This is correct
Now,
delay_countup = 50.5
countup = 50.5
Textview updates as 0.5 //This is incorrect. I need 50.5
This happens sometimes...

It seems that you are never removing the first timer. So when you initialize the second timer you have two timers simultaneously trying to update the UI.
Store the timer as a member variable and check if it's initialized before starting a second one. When the dialog is closed you should cancel() the Timer. So you should also see how to implement methods when the dialog is dismissed - this should call a cleanup method which cancel() and sets the timer to null.
public class DialogTest extends Dialog {
Timer timer;
double countup = 0;
double initial_time = 0;
public DialogTest(Context context){
super(context);
}
#Override
protected void onStart() {
super.onStart();
startUpCounting();
}
#Override
protected void onStop() {
Log.e("b", "timer stopped");
if(timer != null){
timer.cancel();
timer = null;
}
super.onStop();
}
public void startUpCounting() {
delay_for_student.setText("Delay in Arrival");
rootView.invalidate();
Log.e("b", "timer started");
if(timer != null){
timer.cancel();
timer = null;
}
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
countup = 0.0 + initial_time;
if (countup == 0.0) {
onTimeHandler.obtainMessage(1).sendToTarget();
} else {
mHandler.obtainMessage(1).sendToTarget();
}
initial_time = initial_time + 0.5;
}
}, 0, 1000);
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Log.e("b", "timer: " + countup);
delay_time.setText(String.valueOf(countup) + "min");
rootView.invalidate();
}
};
public Handler onTimeHandler = new Handler() {
public void handleMessage(Message msg) {
Log.e("b", "timer ---");
delay_time.setText("-");
rootView.invalidate();
}
};
}

Related

handler.postDelayed not working correctly

I have a simple stopwatch code piece. Thread is running in custom class, it connects to the main activity via Interface
public class MainActivity extends AppCompatActivity implements MainActivityInteractionInterface{
public static boolean isRunning = false;
Stopwatch stopWatch;
private TextView textViewMilliSeconds;
private TextView textViewSeconds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewMilliSeconds = findViewById(R.id.textViewStopwatchMilliseconds);
textViewSeconds = findViewById(R.id.textViewStopwatchSeconds);
stopWatch = new Stopwatch(this, getApplicationContext());
stopWatch.runThread();
}
#Override
public void updateUI() {
String time = String.format(Locale.getDefault(), "%03d", stopWatch.getMilliseconds());
textViewMilliSeconds.setText(time);
String timeSeconds = String.format(Locale.getDefault(), "%02d", stopWatch.getSeconds());
textViewSeconds.setText(timeSeconds);
}
public void startTimer(View view) {
isRunning = !isRunning;
}
public class Stopwatch {
private int milliseconds = 0;
private int seconds = 0;
public int getMilliseconds() {
return milliseconds;
}
public int getSeconds() {
return seconds;
}
private MainActivityInteractionInterface interactionInterface;
private Context applicationContext;
public Stopwatch(MainActivityInteractionInterface interactionInterface, Context applicationContext){
this.interactionInterface = interactionInterface;
this.applicationContext = applicationContext;
}
public void runThread(){
final Handler handler = new Handler();
handler.post(new Runnable(){
#Override
public void run(){
if(isRunning) {
milliseconds++;
if (milliseconds == 1000) {
milliseconds = 0;
seconds++;
if(seconds == 60){
seconds = 0;
}
}
}
interactionInterface.updateUI();
handler.postDelayed(this, 1);
}
});
}
handler should update every 1 millisec, when there is 1000 milliseconds, 1 second passes by
If I set handler.postDelayed delay anything below 15 reaching 1000 milliseconds would take exactly 18 seconds, why?
I don't know why it would take up to 18seconds, but I can tell you this: Android refresh the UI every 16msec (to have a rate of 60fps), so setting the handler to updateUI in a lesser time would make no sense and maybe also interfier with it.
In my humble opinion, make it to update in 20msec and change the counter values according, like this:
handler.post(new Runnable(){
#Override
public void run(){
if(isRunning) {
milliseconds++;
if (milliseconds == 50) {
milliseconds = 0;
seconds++;
if(seconds == 60){
seconds = 0;
}
}
}
interactionInterface.updateUI();
handler.postDelayed(this, 20);
}
});
Look at the second argument of handler.postDelayed(this, 1);
Change it according to the way you increment your milliseconds.

pause a reversed countdown timer in java for android

I'm making a timer which has a button I can control either pick the count-up mode or countdown mode. The count down mode is working perfectly, start, pause, reset. The count-up timer is made by reversing the countdown timer in Android studio. the problem is I can't pause the count-up timer and resume from where it stops.
What's the proper way to do that? I know it could be done by chronometer for count-up mode, but I'm trying to make a customized timer widget that as I mentioned before, a button could be pressed to let the user decide which mode he wants. So if possible, I don't want to use another widget in my current set up.
public void startBuiltInTimer(int millisecondCountingInterval) {
if (getTimeInMillis() > 0) { //getTimeInMillis will read the time value in the current Timer widget, which is a textView
builtInTimerIsCounting = true; //set the timer is running flag
showAsCounting(true);
if (getTimerCountUpOrDown() == TIMER_COUNT_DOWN) { //countdown mode
countDownTimer = new CountDownTimer(getTimeInMillis(), millisecondCountingInterval) {
#Override
public void onTick(long millisUntilFinished) {
setTime(millisUntilFinished);
}
}
#Override
public void onFinish() {
setTime(0);
builtInTimerIsCounting = false;
showAsCounting(false);
}
}
}.start();
} else if (getTimerCountUpOrDown() == TIMER_COUNT_UP) { //count up mode
long tempTimeInMillis = getTimeInMillis() + lastTimeMillisUntilFinished; //trying to save the last time value in millis and keep the total time value as user defined.
setTime(0); //set initial value of the textView to show the count-up timer start from zero, initially.
countDownTimer = new CountDownTimer(tempTimeInMillis, millisecondCountingInterval) {
#Override
public void onTick(long millisUntilFinished) {
long temp = tempTimeInMillis - millisUntilFinished;
setTime(temp); //reverse the countdown to count-up
lastTimeMillisUntilFinished = millisUntilFinished;//keep the millisUntilFinished for next start.
}
#Override
public void onFinish() {
setTime(tempTimeInMillis);
builtInTimerIsCounting = false;
showAsCounting(false);
}
}
}.start();
}
}
}
Edit:
I end up to use SystemClock to keep tracking and record the count up timer. Here is the code.
else if (getTimerCountUpOrDown() == TIMER_COUNT_UP) {
startCountUpTimer(millisecondCountingInterval);
}
public void startCountUpTimer(int millisecInterval) {
if (!countUpTimerIsRunning) {
handler = new Handler();
myRunnable = new MyRunnable(millisecInterval); //MyRunnable can pass a variable thru to let handler have more flexibility with postDelay method. You can still use regular Runnable with hard coded time interval in millisec.
startTime = SystemClock.uptimeMillis(); //use startTime to mark the time when the count up timer start ticking
handler.postDelayed(myRunnable, millisecInterval);
countUpTimerIsRunning = true;
}
}
public class MyRunnable implements Runnable {
private int interval;
public MyRunnable(int t) {
this.interval = t;
}
#Override
public void run() {
millisecondTime = SystemClock.uptimeMillis() - startTime; //this is the time value measured by the count up timer
updateTime = timeBuff + millisecondTime;
setTime(updateTime);
if (updateTime <= countUpMode_Threshold) {
handler.postDelayed(this, interval);
} else {
setTime(countUpMode_Threshold);
handler.removeCallbacks(myRunnable);
builtInTimerIsCounting = false;
showAsCounting(false);
if (onBuiltInTimerStatusChangeListener != null) {
onBuiltInTimerStatusChangeListener.onStatusChange(OnBuiltInTimerStatusChangeListener.STATUS_FINISHED);
}
}
}
}
public void pauseCountUpTimer() {
timeBuff += millisecondTime;
handler.removeCallbacks(myRunnable);
countUpTimerIsRunning = false;
}
One way is that you should keep track of the pause time manually and start timer from that time again. Or you can try using the utility at this link It has the functionality you are looking for.
Pretty helpful
Maybe something like this:
private CountDownTimer timer;
private long COUNT_DOWN = -1;
private long COUNT_UP = 1;
public void startBuiltInTimer(boolean isDown) {
if (isdown) {
launchTimer(COUNT_DOWN);
} else {
launchTimer(COUNT_UP);
}
}
public void launchTimer(long interval) {
builtInTimerIsCounting = true; //set the timer is running flag
showAsCounting(true);
timer = new CountDownTimer(getTimeInMillis(), TimeUnit.SECONDS.toMillis(interval) {
#Override
public void onTick(long time) {
setCountdownText(time.intValue());
setTimeInMillis(time);
}
#Override
public void onFinish() {
setCountdownText(time.intValue());
builtInTimerIsCounting = false;
showAsCounting(false);
}
}.start();
public void stopTimer() {
timer.cancel();
}
...

Java/Android Countdown timer

i'm trying to make an countdown timer on Android with java. There are a timer a 25 minute study timer and a 5 minute break timer. I have both timers working. However, I don't know how to cancel/clear the timers. If I click I start a study timer and break timer they will both run simultaneously and I don't want that. I want the original timer to cancel when another timer is put on. This is the code.
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View v){
btnStart.setEnabled(false);
btnBreak.setEnabled(true);
breakBoolean = false;
CountDownTimer timer;
long amountOfStudyTime = 1500000; //30 seconds (may cause problems)
long countDownInterval = 1000; //1 second
//Initialise the countdown timer
timer = new CountDownTimer(amountOfStudyTime, countDownInterval){
public void onTick(long millisUntilFinished){
if( studyBoolean = false ) {
tViewTime.setText("CountDownTimer Canceled/stopped.");
cancel();
breakBoolean = true;
}else{
//display remaining seconds to user
tViewTime.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
}
public void onFinish(){
//When countdown is finished do...
breakBoolean = true;
int currentScore = Integer.parseInt(editScore.getText().toString());
int finalScore = currentScore + 5;
editScore.setText(Integer.toString(finalScore));
tViewTime.setText("Done");
}
}.start();
}
});
//Set a click listener for break button
btnBreak.setOnClickListener(new OnClickListener() {
public void onClick(View v){
btnStart.setEnabled(true);
btnBreak.setEnabled(false);
studyBoolean = false;
CountDownTimer timer2;
long amountOfBreakTime = 300000; //30 seconds (may cause problems)
long countDownInterval = 1000; //1 second
//Initialise the countdown timer
timer2 = new CountDownTimer(amountOfBreakTime, countDownInterval){
public void onTick(long millisUntilFinished){
if( breakBoolean = false ) {
cancel();
studyBoolean = true;
}else{
//display remaining seconds to user
tViewTime.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
}
public void onFinish(){
//When countdown is finished do...
tViewTime.setText("Done");
studyBoolean = true;
}
}.start();
}
});
Create two CountDownTimer objects in your activity and cancel them depending on your button selected :
public class MainActivity extends Activity {
boolean breakIsRunning = false;
boolean startIsRunning = false;
Button btnStart,btnBreak;
CountDownTimer startTimer = new CountDownTimer(amountOfStudyTime, countDownInterval)
{
#Override
public void onFinish() {
//do something
startIsRunning = false;
}
#Override
public void onTick(long arg0) {
//do something
startIsRunning = true;
}
};
CountDownTimer breakTimer = new CountDownTimer(amountOfBreakTime, countDownInterval)
{
#Override
public void onFinish() {
//do something
breakIsRunning = false;
}
#Override
public void onTick(long arg0) {
//do something
breakIsRunning = true;
}
};
//->OnCreate() - >Buttons code
btnStart.setOnClickListener(new OnClickListener().. { // your listener code here
if(breakIsRunning)
breakTimer.cancel();
startTimer.start();
}
btnBreak.setOnClickListener(new OnClickListener().. { //
if(startIsRunning)
startTimer.cancel();
breakTimer.start();
}
}

updating notification text every second with handler

i've a strange issue. i want to update the text of a notification every second so i wrote this code
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
batteryTemperature = getBatteryTemperature();
new Handler().postDelayed(this, 1000);
}
}, 1000);
if(batteryTemperature != 0) {
builder.setContentText("BatteryLevel"+batteryTemperature);
} else {
builder.setContentText("The batteryTemperature value is 0");
}
With a Log i saw that in the Handler the batteryTemperature value is different from 0 but out of the Handler it return 0. Why? How can i set the correct value in the notification?
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
batteryTemperature = getBatteryTemperature();
handler.postDelayed(this, 1000);
}
}, 1000);
Do not create a Handler every time the Runnable is called. You need to executed this snippet, every time the runnable is executed
if(batteryTemperature != 0) {
builder.setContentText("BatteryLevel"+batteryTemperature);
} else {
builder.setContentText("The batteryTemperature value is 0");
}
Try this (UI thread safe):
TimerTask task = new TimerTask()
{
private final Handler mHandler = new Handler(Looper.getMainLooper());
#Override
public void run()
{
mHandler.post(new Runnable()
{
#Override
public void run()
{
batteryTemperature = getBatteryTemperature();
if (batteryTemperature != 0)
{
builder.setContentText("BatteryLevel" + batteryTemperature);
}
else
{
builder.setContentText("The batteryTemperature value is 0");
}
}
});
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 1000);
There are two things you're asking: 1) how do I update my notification and 2) why is the value always 0?
Let me answer the second one first, try this code instead:
private MyHandler myHandler = new MyHandler();
...
// get the ball rolling...
myHandler.sendEmptyMessageDelayed(MyHandler.UPDATE, 1000);
...
private class MyHandler extends Handler {
private int UPDATE = 0;
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE:
updateBatteryTemperatureNotification(getBatteryTemperature());
sendEmptyMessageDelayed(MyHandler.UPDATE, 1000);
break;
case STOP:
removeMessages(UPDATE);
break;
}
super.handleMessage(msg);
}
}
Is it safe to assume you know how to update the Notification when you have the data correct?

Android: Pause/Resume Timer OR Thread

I have checked all SO answers about how to pause/resume timer, but can't find a solution.
I have created a Timer task which counts the effort time for an employee and puts it inside a TextView to show.
Code below:
Timer T = new Timer();
T.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
String workingTime = "Your effort is "
+ format.format(Double.valueOf(hr)) + ":"
+ format.format(Double.valueOf(min)) + ":"
+ format.format(Double.valueOf(sec))
+ " till now for the day";
storeEffort.setText(workingTime);
sec++;
if (sec > 59) {
sec = 0;
min = min + 1;
}
if (min > 59) {
min = 0;
hr = hr + 1;
}
}
});
}
}, 1000, 1000);
where storeEffort is my TextView which shows the effort time which is stuck inside the running thread(main problem). I want to pause the effort timer with a button click and resume it when the same button clicked again.Is there any other way to do this kind of task?
You solution might have a slight problem - you are using timer to count time intervals whereas there is no need to. You could use i.e. StopWatch to count elapsed time. So instead of adding seconds in a timer job you could just get elapsed time from this timer. To pause the timer you could call stopWatch.stop() and to start it, you could call stopWatch.start().
It could look like this:
Stopwatch stopwatch = Stopwatch.createStarted();
void startThreadUpdateTimer(){}
Timer T = new Timer();
T.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
String workingTime = "Your effort is " + sw.toString() +
" till now for the day";
}
});
}
}, 1000, 1000);
}
public void pause(){
if(stopwatch.isRunning()){
stopwatch.stop();
}
}
public void resume(){
if(!stopwatch.isRunning()){
stopwatch.start();
}
}
UPDATE Solution if the timer needs to start from beginning every second time:
public class YourOuterClass extends Activity {
private YourTimerTask mTimerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button;
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mTimerTask != null && mTimerTask.isTaskActive()) {
mTimerTask.deactivateTimer();
mTimerTask = null;
} else {
startTask();
}
}
});
...
}
private class YourTimerTask extends TimerTask {
private boolean mIsTimerActive;
public YourTimer() {
mIsTimerActive = true;
}
public void deactivateTimer() {
mIsTimerActive = false;
}
public boolean isTaskActive() {
return mIsTimerActive;
}
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
String workingTime = "Your effort is "
+ format.format(Double.valueOf(hr)) + ":"
+ format.format(Double.valueOf(min)) + ":"
+ format.format(Double.valueOf(sec))
+ " till now for the day";
if (!mIsTimerActive) {
cancel(); // will cancel this timer instance
}
sec++;
if (sec > 59) {
sec = 0;
min = min + 1;
}
if (min > 59) {
min = 0;
hr = hr + 1;
}
}
});
}
}
...
private void startTask() {
Timer T = new Timer();
mTimerTask = new YourTimertask();
T.scheduleAtFixedRate(mTimerTask, 1000, 1000);
}
}

Categories