I want to log when something happened and delay 1 minitues. so I use a timer and timertask live this:
private Timer _checkTimer;
private boolean _timerStart = false;
private void requestPlayPosterItem(long id) {
//
if(_timerStart && _checkTimer != null ){
_checkTimer.cancel();
}
_checkTimer = new Timer();
_checkTimer.schedule(new TimerTask(){
#Override
public void run() {
_timerStart = false;
// do log
}
}, 30000);
_timerStart = true;
// others -----
}
and at activity onPause(), I cancel the timer too:
if(_checkTimer != null && _timerStart){
_checkTimer.cancel();
}
my question: _checkTimer can reuse? is there resource leak(timer)?
Related
I am working on a stopwatch app which has a start button. After clicking on the start button, the timer starts and button text is updated to Pause. After clicking on Pause, the timer stops and button text changes to Resume. When I click on resume, I expect the timer to start from where it paused, but the timer jumps to few seconds forward. Below is the code snippet:
#Override
protected void onCreate(Bundle savedInstanceState) {
this.timerHandler = new Handler();
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!MainActivity.running && !MainActivity.paused) {
MainActivity.running = true;
MainActivity.paused = false;
MainActivity.startTime = System.currentTimeMillis();
startTimer();
startButton.setText("PAUSE");
}
else if (MainActivity.running && !MainActivity.paused) {
MainActivity.running = false;
stopTimer();
MainActivity.paused = true;
MainActivity.pauseTime = System.currentTimeMillis();
MainActivity.pauseTimerText = timerText(); // Eg: 00:00:08
startButton.setText("RESUME");
}
else if(MainActivity.paused) {
MainActivity.running = true;
MainActivity.paused = false;
startTimer();
startButton.setText("PAUSE");
}
resetButton.setVisibility(View.VISIBLE);
}
});
}
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
if (!MainActivity.running) {
return;
}
if (pauseTimerText != null)
{
MainActivity.timerTV.setText(pauseTimerText); // this gets set to 00:00:08 here
pauseTimerText = null;
}
else
{
MainActivity.timerTV.setText(timerText());
}
timerHandler.postDelayed(timerRunnable, 100); // this changes the timer text.
}
};
void startTimer() {
MainActivity.running = true;
timerRunnable.run();
}
void stopTimer() {
MainActivity.running = false;
timerHandler.removeCallbacks(timerRunnable);
timerHandler.removeCallbacksAndMessages(null);
}
If my pauseTimerText is 00:00:08, then in runnable "MainActivity.timerTV.setText(pauseTimerText);" line does set the timer text to 00:00:08, but I noticed that after postDelayed gets executed, instead of timer showing 00:00:08 on UI, it would show something like 00:00:13 i.e. it also adds up and shows the time it was paused.
Looks like either postDelayed is not working right or removeCallBacks is not removing everything.
I'm designing a game and have a problem with countdowntimer not stopping. The timer starts normally as it should, and should stop in the end() method. It works well when ENEMY_HULL is checked as 0 or less, which is calulated seperatly when the users presses a button, and checkEnemyDefeat() is called, but it does not work when ENEMY_CREW is 0. On both situations the message in log cat that the timer is stopped appears, so my guess is that .cancel() is called. I've included all code that seems relevant.
NOTE: All variables including CountDownTimer are global variables
Here is the method containing the timer:
private void BadGameLoop() {
if (GAME_PAUSED == true && !GAME_TIME_STARTED) {
Log.d(" -------- ", "TIMER STARTED");
GAME_PAUSED = false;
GAME_TIME_STARTED = true;
gameTimer = new CountDownTimer(GAME_TIME, UPDATE_RATE) {
#Override
public void onTick(long millisUntilFinished) {
TICKS++;
timer.setText(String.valueOf(GAME_TIME / 1000 - TICKS / 10));
if((CREW_BOARDING > 0 || ENEMY_CREW_BOARDING > 0) && TICKS%10 == 0){
crewFightCalculator();
}
updateViews();
}
#Override
public void onFinish() {
TICKS=0;
GAME_TIME_STARTED = false;
GAME_PAUSED = true;
end();
}
};gameTimer.start();
}
}
Next is the crewFightCalculator method:
private void crewFightCalculator(){
// this just changes the values of ENEMY_CREW (global variable) and then
//calls checkEnemyDefeat() to verify if it should end the timer or not
checkEnemyDefeat();
}
The checkEnemyDefeat and end methods:
private void checkEnemyDefeat(){
if(ENEMY_HULL <= 0){
updateViews();
end();
}else if ( ENEMY_CREW < 1){
updateViews();
end();
}
}
private void end(){
if(GAME_TIME_STARTED){
GAME_TIME_STARTED = false;
gameTimer.cancel();
Log.d("---------"," TIMER STOPPED !");
}
// do more stuff
}
It is simply bizzare to me why it dosen't work. My only guess is that it has something to do with the fact that .cancel() is not triggered by a chain of functions started by user input .
The only solution to this problem that I found, was to modify the onTick method, so that it should check every time if it should run or stop the timer like this:
private void BadGameLoop() {
if (GAME_PAUSED == true && !GAME_TIME_STARTED) {
Log.d(" -------- ", "TIMER STARTED");
GAME_PAUSED = false;
GAME_TIME_STARTED = true;
gameTimer = new CountDownTimer(GAME_TIME, UPDATE_RATE) {
#Override
public void onTick(long millisUntilFinished) {
if(GAME_TIME_STARTED) {
TICKS++;
timer.setText(String.valueOf(GAME_TIME / 1000 - TICKS / 10));
if((CREW_BOARDING > 0 || ENEMY_CREW_BOARDING > 0) && TICKS%10 == 0){
crewFightCalculator();
}
updateViews();
}else{
gameTimer.cancel();
}}
#Override
public void onFinish() {
TICKS=0;
GAME_TIME_STARTED = false;
GAME_PAUSED = true;
end();
}
};gameTimer.start();
}
}
Still, i can't figure out why it didn't work properly before.
I have an animation in my Android app that flashes a TextView different colors. I've used a TimerTask, Timer, and Runnable method to implement this. What I need to do is stop the thread when a user leaves the app during this animation in onPause(), and resume the thread when the user returns to the app in onResume(). The following is the code I've implemented, but it's not working (the onPause(), and onResume() pieces), and I don't understand why. I've read a few other posts on similar matters, but they haven't helped me figure out what to do in my situation. I've read that TimerTasks are outdated, and I should probably use an ExecutorService method; it is unclear to me as how to implement this function.
...timerStep5 = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (b5) {
cashButton2SignalText.setBackgroundColor(Color.RED);
cashButton2SignalText.setTextColor(Color.WHITE);
b5=false;
} else {
cashButton2SignalText.setBackgroundColor(Color.WHITE);
cashButton2SignalText.setTextColor(Color.RED);
b5=true;
}
}
});
}
};
timer5.schedule(timerStep5,250,250);
}
public void onPause(){
super.onPause();
timerStep5.cancel();
}
public void onResume(){
super.onResume();
timerStep5.run();
}
After a TimerTask is canceled, it cannot run again, you have to create a new instance.
Read details here:
https://stackoverflow.com/a/2098678/727768
ScheduledThreadPoolExecutor is recommended for newer code, it handles the cases like exceptions and task taking longer time than the scheduled interval.
But for your task, TimerTask should be enough.
Here's how I did it. Add pauseTimer boolean where ever the pause takes place (button listener perhaps) and don't count timer if true.
private void timer (){
Timer timer = new Timer();
tv_timer = (TextView) findViewById(R.id.tv_locationTimer);
countTimer = 0;
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
String s_time = String.format("%02d:%02d:%02d",
countTimer / 3600,
(countTimer % 3600) / 60,
countTimer % 60);
tv_timer.setText(s_time);
if (!pauseTimer) countTimer++;
}
});
}
}, 1000, 1000);
}
Timer timer1;
private boolean timerStartFlag = false;
private boolean hiddenVisibleFrg = false;
int timerSize = 0;
int videoTime = 0;
#Override
public void onPause() {
super.onPause();
Log.e("keshav", "onPause timer1 " +timer1);
if (timerSize >0 &&hiddenVisibleFrg){
timerStartFlag =true;
}
if (timer1 != null) {
this.timer1.cancel();
}
}
#Override
public void onResume() {
super.onResume();
if (timerSize >0 && timerStartFlag && hiddenVisibleFrg) {
callTimerTask(timerSize);
timerStartFlag = false;
}
}
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
Log.e("keshav", "HomeFragment visible ");
if (timerSize >0 && timerStartFlag) {
callTimerTask(timerSize);
timerStartFlag=false;
}
hiddenVisibleFrg=true;
} else {
Log.e("keshav", "HomeFragment in visible " +timer1);
if (timer1 != null) {
this.timer1.cancel();
}
if (timerSize >0){
timerStartFlag =true;
}
hiddenVisibleFrg=false;
}
}
private void callTimerTask(int size) {
// TODO Timer for auto sliding
printLog("callTimerTask size " + size);
timer1 = new Timer();
timer1.schedule(new TimerTask() {
#Override
public void run() {
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (getActivity() == null) {
return;
}
if (count1 < size - 1) {
//TODO ADD ME kk
count1++;
} else {
count1 = 0;
}
if (intro_images != null) {
intro_images.setCurrentItem(count1);
}
videoTime++;
Log.e("KeshavTimer", "callTimerTask videoTime " + videoTime);
}
});
} else {
printLog("callTimerTask getActivity is null ");
}
}
}, 1000, 1000);
// TODO 1000, 3000;
}
For Kotlin user, checkout this
How to use:
// Init timer
lateinit var timerExt: CountDownTimerExt
timerExt = object : CountDownTimerExt(TIMER_DURATION, TIMER_INTERVAL) {
override fun onTimerTick(millisUntilFinished: Long) {
Log.d("MainActivity", "onTimerTick $millisUntilFinished")
}
override fun onTimerFinish() {
Log.d("MainActivity", "onTimerFinish")
}
}
// Start/Resume timer
timerExt.start()
// Pause timer
timerExt.pause()
// Restart timer
timerExt.restart()
I have used Countdown Timer like this
new CountDownTimer(15000, 15) {
public void onTick(long millisUntilFinished) {
long seconds=millisUntilFinished/1000;
long min=millisUntilFinished%100;
timeleft=(int) (seconds*1000+min);
if(millisUntilFinished>=10000)
{
changeText.setTextColor(Color.GREEN);
}
else if(millisUntilFinished>=5000)
{
changeText.setTextColor(Color.MAGENTA);
}
else
{
changeText.setTextColor(Color.RED);
}
changeText.setText(String.format("%02d", seconds )+ "."+String.format("%02d", min )+" sec");
}
public void onFinish() {
timeleft=0;
missed++;
nametext.setTextColor(Color.RED);
nametext.setText("Time Up!");
bottombutton.setVisibility(View.INVISIBLE);
globalflag=13;
changeText.setTextColor(Color.RED);
changeText.setText("0.00 Sec");
Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnablecif, 3000);
}
}.start();
On a button click I have called cancel() but it stops counting for a while and then calls onFinish(). I need not to call onFinish() after calling cancel(). Is there any solution for this. Any help will be highly appreciated.
Inside your onClick set a Boolean (buttonPressed for example) to true.
In your onFinish check this Boolean:
if (buttonPressed == true)
{
//do nothing
}
else
{
//run code
}
You could use Timer instead and do something like this:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
// do your updates here
mUpdateTimeHandler.postDelayed(this, 1000);
}
};
Handler mUpdateTimeHandler = new Handler();
mUpdateTimeHandler.postDelayed(mUpdateTimeTask, 100);
When cancelling the task:
mUpdateTimeHandler.removeCallbacks(mUpdateTimeTask);
I also don't understand why the onFinish gets called despite calling cancel(). I guess the only workaround for this is to just null check your calls in onFinish to prevent any NPE's.
public void onFinish() {
if (nametext == null || bottombutton == null || changeText == null || ... ) {
return;
}
...
I have been running through alot of issues try to pause and unpause a timer, and if I lock the orientation to portrait or landscape it works, but thats not exactly what I want to do. Of course, the onCreate method is called when you change orientation, so im canceling my timertask and setting it to null, but after running through the orientation more than once, it doesnt cancel the timertask anymore. Ive looked through other peoples questions on here but none seem to hold the answer to my quesiton. Heres my code. Its a little sloppy at the moment because ive been trying about everything I can to get it to work.
public class singleTimer extends Activity implements OnClickListener {
private Integer setTime = 0;
private Integer tmrSeconds = 0;
private Integer tmrMilliSeconds = 0;
private Timer myTimer = new Timer();
private TimerTask myTimerTask;
private TextView timerText;
private boolean isPaused = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_timer);
Bundle extras = getIntent().getExtras();
setTime = extras.getInt("com.bv.armyprt.timer_duration");
if (myTimerTask != null) {
myTimerTask.cancel();
myTimerTask = null;
}
if (savedInstanceState != null) {
if (savedInstanceState.getInt("tmrSeconds") == 0) {
tmrSeconds = setTime;
} else {
tmrSeconds = savedInstanceState.getInt("tmrSeconds");
tmrMilliSeconds = savedInstanceState.getInt("tmrMilliseconds");
if (isPaused == false) {
myTimer = new Timer();
myTimerTask = new TimerTask() {
#Override
public void run() {
TimerMethod();
}
};
myTimer.schedule(myTimerTask, 0, 100);
}
}
} else {
tmrSeconds = setTime;
}
timerText = (TextView)findViewById(R.id.timerText);
timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
TextView timerDesc = (TextView)findViewById(R.id.timerDescription);
timerDesc.setText("Timer for: " + setTime.toString());
Button startButton = (Button)findViewById(R.id.timerStart);
Button stopButton = (Button)findViewById(R.id.timerStop);
Button closeButton = (Button)findViewById(R.id.timerClose);
closeButton.setOnClickListener(this);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case (R.id.timerStart):
isPaused = false;
myTimer = new Timer();
myTimerTask = new TimerTask() {
#Override
public void run() {
TimerMethod();
}
};
myTimer.schedule(myTimerTask,0, 100);
break;
case (R.id.timerStop):
isPaused = true;
myTimerTask.cancel();
myTimerTask = null;
myTimer.cancel();
break;
case (R.id.timerClose):
onDestroy();
this.finish();
break;
}
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.
tmrMilliSeconds--;
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
if (tmrSeconds > 0) {
if (tmrMilliSeconds <= 0) {
tmrSeconds--;
tmrMilliSeconds = 9;
}
} else {
Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
myTimer.cancel();
tmrSeconds = setTime;
tmrMilliSeconds = 0;
isPaused = true;
}
//Do something to the UI thread here
timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
}
};
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("setTimer", setTime);
savedInstanceState.putInt("tmrSeconds", tmrSeconds);
savedInstanceState.putInt("tmrMilliseconds", tmrMilliSeconds);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
setTime = savedInstanceState.getInt("setTimer");
tmrSeconds = savedInstanceState.getInt("tmrSeconds");
tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");
}
}
you can simply add a boolean variable
boolean stopTImer = false ;
and in your timerTask , do something like this :
#Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}
and when you want to stop it , put the boolean to true
You should stop the timer during onStop. Android might create another instance of your Activity and you will lose the reference to your previous timer(task) when you change orientation.
All objects tied to an activity follow the activity lifecycle. That means you have to store the references to objects elsewhere if you want to keep them even if the activity gets deleted (which can happen quite often).