I have Separated CountDownTimer in every activity in my app, but the problems happen when I switch between the activities , there is delay some milliseconds and I need the switching between the activities becomes directly without any delay. for instance , I'm in the activity 1 which has CountDownTimer , when I press the button to go to the second activity that has CountDownTimer also , it takes some milliseconds to bring the activity 2. and this is my code
countDownTimer21 = new CountDownTimer(6000, 1000) {
public void onTick(long millisUntilFinished) {
strLong = Long.toString(millisUntilFinished / 1000);
time.setText(strLong);
}
public void onFinish() {
Intent fail = new Intent(Test10_D.this, FailPage10.class);
fail.putExtra("scorerecord", myscore);
next.putExtra("scorevalue", strLong);
startActivity(fail);
countDownTimer21.cancel();
//finish();
}
}.start();
Any suggestions? Thanks Alot
I think You have created two countdown timers in two different activity and when you go from 1st->2nd activity first timer is stooped and 2nd is resumed from value provided by 1st activity's timer.
So Solution is write single common timer in a separate thread like this:
Write your Countdown Timer in a separate service and show the countdown on UI through handler of activity which is on screen.
that's it
Related
I need to create an alarm every time new data is inserted on the Database(the database has the values of date and time of every alarm that is going to be created), without any interaction with the user, the app has to do it automatically.
How do I know when new data is inserted? checking every x minutes using a timer?
How to create an alarm automatically when that happens?
Typically for something like this, you would want to use something more like Firebase that will live update your data.
But to do a timer, first extend TimerTask:
public class Progress extends TimerTask {
#Override
public void run(){
//this item runs on a separate thread
runOnUiThread(new Runnable() {
#Override
public void run() {
seconds += 0.1;
//this item runs on the main thread
}
});
}
}
And then to run this task:
Timer timer;
Progress progress;
timer.schedule(progress, 100, 100);
This will run the timer every .1 seconds.
In my UI I have a button and a label.
On the button click I want to start a new thread which would update the label text to display the timer information (like 00:00 seconds).
This should be updated every second.
Could someone give a simple solution to this problem please?
Assuming that the information is in the form of a textView (called tv), you would use a countdowntimer:
new CountDownTimer(30000, 1) {
public void onTick(long millisUntilFinished) {
mTextField.setText(/* Whatever you want for each millisecond */);
}
public void onFinish() {
mTextField.setText("00:00");
}
}.start();
the second parameter in the CountDownTimer constructor is the intervals for which onTick, so for this timer, onTick will be called every millisecond.
Any questions?
I have a button on which I want to set the timer for 5 seconds for the first time and it should perform some task after completing 5 seconds. Also if user click button 2 times it should start timer for 10 seconds and after 10 seconds it should perform specific task. and if user click 3rd time it should stop all running timers. so I have do not know How to implement timer for one time
what I have search is this. But in this link it is continuously repeating after specific period of time, whereas I want to run once.
Now what I want
To start timer with first click (of 5 seconds)and if meanwhile user click 2nd time it should set timer with with new time period and if user click third time it cancels out all timers.
I do not want to use Thread timer using sleep method.
I want same behavior as there is in camera app in android 5.0 v.
So please tell me how to do this any code and source code would be appreciated.
In the link you provided you will find the answer if you try little harder.
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after);
So what you need to do is to maintain temporary variable to keep track of number of clicks and you can use second method like
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after * numberOfTimeBtnClked);
You have to pass the TimerTask method instead of task in that method.
**For updating your textview use below code and forget about whatever I have written above **
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//run in an interval of 1000ms
timer.schedule(timerTask, 0, 1000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
timerSince++; //global integer variable with default value 0
if(timerSince == 5 * numberOfBtnClick){
//call your method
timer.cancel;
timerSince = 0;
}else{
//textView.setText(((5 * numberOfBtnClick)-timerSince)+" second left");
}
});
}
};
}
}
On event start button click call:
startTimer();
I'm making an application and a certain part in my application I store a list of prices along with the time that the prices last for (they do not all last the same amount of time). So a price lasts a certain time then once that price's time is up it changes to another price (this part changes the UI or basically it updates a textview with the new price). So what I need is a timer that sets the timer again with the new time length and once it's done make the UI change. For instance say that each of the pairs represent the price amount and the time (in seconds): { {$2.53,1.4s}, {$4.57,4.45s}, {$1.23,3.6s}...}
So when the timer starts off the textview displays $2.53 and the timer lasts 1.4s and then it should grab the next price $4.57 and be set again but this time for 4.45s. This process continues on and on until the game is finished. I was thinking of using the CountDownTimer and resetting itself once the onFinish() method is called (I haven't verified if this idea works yet). Are there any other ideas?
You can use a countdown timer and onFinish method you call back the function and it starts another timer:
private void startWheatPrices(long gameTime)
{
//other stuff executed
StartWheatTimer(GameTimeDifference);//starts the timer for the first time
}
private void StartWheatTimer(long TimerAmount)
{
WheatTimer = new CountDownTimer(TimerAmount, 10) {
public void onTick(long millisUntilFinished)
{
}
public void onFinish()
{
//other stuff executed
WheatPricesTV.setText(Float.toString(PriceList.get(0).get(WheatPriceIndex).price));//price is changed
if(InGameplayMode)
StartWheatTimer(convertToMilliseconds(PriceList.get(0).get(WheatPriceIndex).timeLength));//call back the function to start the timer again
}
}.start();
}
Im making a game on android and i want it to run for like 60 seconds and the display game over. much like fruit ninja. how do i handle the time in this case? is the android.timer of much use or the alarm manager?
You can use a CountDownTimer:
private void initTimer() {
mWaitTime = 60 * 1000;
mWaitTimer = new CountDownTimer(mWaitTime, mWaitTime) {
public void onTick(long millisUntilFinished) {/* Do nothing */ }
public void onFinish() {
// stop your game...
}
}.start();
}
Be sure to cancel the timer on the onPause method:
mWaitTimer.cancel();
Use an AsyncTask, run a Thread with the sleep method (in the doInBackground method) for 1 second do this 60 times and after each second you can update the progress so the method onProgressUpdate will be called each second, after the thread has runned 60 times the AsyncTask will go into the onPostExecute where you can finish your application or do whatever you want to do when the time has run out.