I have defined a chronometer;
protected Chronometer chrono;
protected int baseTime;
protected int stopTime;
protected long elapsedTime;
My program asks questions to the user and i want to set a timer based on the user's input. I want a timer which starts at 10 to 0. How can i do that?
Also, I want to show the remaining time on the screen.
Use CountDownTImer instead,
new CountDownTimer(10000, 1000) { //Sets 10 second remaining
public void onTick(long millisUntilFinished) {
mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextView.setText("done!");
}
}.start();
Note : It's not possible to start Chronometer reversely because the Chronometer widget only counts up.
Edit: From API level 24, it is possible to perform count down using chronometer via help of Chronometer#setCountDown(true) method.
Related
I am building an app for video editing...
I want to give some free minutes for 1st time using of app . User can edit the videos until the completion free minutes .
These minutes count based on duration of video which is editing..
After that they have to purchase for further any duration of videos..?
How can I limit user to use some until free minutes?
Till now I didn't used any online service in my app. Everything worked on device.
Any way to implement it on offline.?
If not possible..
What can I do to tackle it..?
I not a native English speaker..
Try to understand... :)
This is a timer that will trigger when no time is left.
It works completely offline .
This goes in the Class outside any other method where you usually declare your variables: (Note that you can set the time you want in millis. I chose 10000ms so 10 seconds as an example)
private CountDownTimer timer;
private long secondsLeft = 10000; // how much milliseconds of
// free time you want to have
//
And this goes where you want to start the time.
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! User has spent the time
}
}.start();
Then add these (or if you already have these methods then add the code in them to your ones.
#Override
protected void onPause() {
super.onPause();
timer.cancel();
}
#Override
protected void onResume() {
super.onResume();
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! The user has used the app for your amount of time
}
}.start();
}
Note: To make it survive an app restart or a phone restart you have to save "secondsLeft" to your device and load it when the app starts.
Hi I'm working on an app with a countdown timer. When the button is clicked the timer starts based on the user inputs into the text field,when I change the value of the text field and a new value for the timer starts, it flashes and shows the previous countdown value and counts both the values down,flashing between the previous and the current countdown value. How do I get the timer to forget the value from before and only use the current timer value.
I tried using the cancel function however that didn't work, I think it's something in my tick function however I'm not sure what it is.
Here is my code:
CountDownTimer mcountDownTimer = new CountDownTimer(getmonTime(), 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
long hr1=TimeUnit.MILLISECONDS.toHours( millisUntilFinished);
long sub1=hr1*60;
long min1=TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
long sub2=min1*60;
timer.setText(""+String.format("%d hr,%d min, %d sec",
TimeUnit.MILLISECONDS.toHours( millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)-sub1,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-sub2,
-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
timer.setText("0");
}
};
mcountDownTimer.start();
}
};
You probably don't cancel the previous one.
Save the instance of the CountDownTimer mcountDownTimer (BTW rename it to mCountDownTimer)
And before setting a new one, cancel the old one using:
mCountDownTimer.cancel();
and only then create the new instance.
E.g:
CountDownTimer mCountDownTimer;
#Override
public void onCreate...
...
if (mCountDownTimer != null) mCountDownTimer.cancel();
mCountDownTimer = new CountDownTimer...
I am new to android and i am trying to implement a simple timer.For example i have one button and every time i click this button, a dialog shows up and i can set the time.
This time should then be displayed on the same activity, where the button is.
I am fairly new to android and i have only a button on my main activity.
My Questions now : How can i dynamicly add "countdowns" to my main_activity.Lets say maximum is 3.Is there something like a countdown class already or does TimePicker this for me ?
Below code runs a timer for 30 seconds. If you want the user to choose time, you can use an EditText to get the time from user and put it instead of 30000 in below code.
final CountDownTimer timer;
timer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timerText.setText("seconds remaining: " + String.valueOf(millisUntilFinished / 1000));
}
public void onFinish() {
timerText.setText("done!");
}
};
I have one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs
In my application one option is there which is used to select the application trigger time that is implemented in RadioButtons like : 10 mins, 20Mins, 30mins and 60mins
after selecting one of them the application starts at that time.. up to this working fine.
But, now I want to display a count down clock after selecting one option from above from that time onwords one clock, I want to display
That will useful to user how much time remaining to trigger the application
if any body knows about this please try to help me
Thanks for reading
Something like this for short counts:
public void timerCountDown(final int secondCount){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if (secondCount > 0){
//format time and display
counterBox.setText(Integer.toString((secondCount)));
timerCountDown(secondCount - 1);
}else{
//do after time is up
removeCounterViews();
}
}
}, 1000);
}
But this one is even better and it has two events which update the main UI thread: on tick and on finish, where tick's lenght is defined in the second parameter:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();