So I am trying to display the time passed since pressing a button on my app.
My code is:
/*This will initiate the timer*/
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
start=System.currentTimeMillis();
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
The app runs but when I press the button it just shows "0.0".
The app doesn't close out. Any help is appreciated!
Please try this:
/*This will initiate the timer*/
timer = new Timer();
start=System.currentTimeMillis();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
In your code, the value of start is changed everytime the timer elapsed (start = System.currentTimeMillis()), so the value of time is always 0 (System.currentTimeMillis() - System.currentTimeMillis() should be 0 if it is called with no, or very small time difference...). So you should set the value of start on button press, then calculate the difference, and update text view in your timer task.
Related
I want to use CountDownTimer within a for loop but when I am using following code then CountDownTimer is running only once while I want to run it CountDownTimer as per given condition in for loop. it might be a silly question but I will be very thankful to you if I get some help. Thanks in Advance
for (int i=1;i<=10;i++){
Random random = new Random();
totalques.setText(String.valueOf(i) + "/10");
firstnum.setText(String.valueOf(random.nextInt(100)));
secondnum.setText(String.valueOf(random.nextInt(50)));
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
time.setText(String.valueOf(millisUntilFinished / 1000)
+"s");
}
#Override
public void onFinish() {
}
}.start();
}
You can use the Timer class for doing your job
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
//one second elapsed
}
});
}
};
timer.schedule(timerTask, 0, 1000);
The schedule method say start directly and I want to be notified when every 1000 milliseconds elapse.
Don't forget to cancel the timer when you want to stop it with timer.cancel()
Inside the run you can decrement an int and when it reach 0 you can stop the timer
hi guys i want to update my text while my mediaplayer is playing
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
running.setProgress(mediaPlayer.getCurrentPosition());
TextOfMaxValue.setText(mediaPlayer.getDuration());
}
}, 0, 1000);
and when i use settext inside my timer like my code above my app crashes and do not start
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
running.setProgress(mediaPlayer.getCurrentPosition());
}
}, 0, 1000);
it works well with the code above but i want to update my text too .
You need to pass a String argument to your TextView, not an int.
Use:
String durationText = String.valueOf(mediaPlayer.getDuration());
TextOfMaxValue.setText(durationText);
I want to do a cookie clicker like app and i need a simple incrementation over time function.
But i would only want the int to start increasing once i have pressed a button.
I tried this but does not work properly.
int delay = 5000;
int period = 1000;
int count = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
count++;
score.setText(String.valueOf(count));
}
}, delay, period);
The reason its not working is because run() is running on separate Thread, not on UIThread. You need to run setText in UIThread. see the code below
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
count++;
score.setText(String.valueOf(count));
}
});
}
}, delay, period);
I use this code to run my code regularly ,
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//my code here
}
});
}
}, 0, 50);
There is a parameter of timer receives Date object to run code in specifec date, But
I need to Run my code with timertask every friday of week,
Any way?
Afakomoallah, Best regards.
I have setup a stop watch using the com.apache.commons library and the stop watch seems to work fine. What I don't know how to do is display this stopwatch in a textView in my app. In general, I have no idea how that would work, i.e. How exactly would a stopwatch be displayed in a textView, given that the time on a stopwatch keeps changing constantly? At the moment, I have the code below and it updated the text in the textView every second for about 2 seconds and then I got a weird error. I'm not even sure if this is the right way to go about doing this. Please help!
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask() {
#Override
public void run() {
timeText.setText(time.toString());
}
};
timer.schedule(timerTask, 0, 1000);
The error I got after 2 seconds (and it successfully updated the time) was :
"only the original thread that created a view hierarchy can touch its views"
You can only update a TextView on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
//stuff that updates ui
}
});
Your code becomes
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
timeText.setText(time.toString());
}
});
}
};
timer.schedule(timerTask, 0, 1000);
You may have to do myActivityObject.runOnUiThread() if you're getting an error there.
See this for more detail.
To update a view from another thread, you should use handler.
private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
//Change the condition for while loop depending on your program logic
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
timeText.setText(time.toString());
}
});
}
}
};
new Thread(runnable).start();
}