I'm a beginner in android. Now i'm working on a simple app. I want to create a timer in app. I want it to countdown from 10 to 0 (it will be visible to the user) and when it is 0, it should do smth. It should start counting down when onTouch event is called. I tried this way, but it doesn't work. Can anyone help please?
here's my code:
final MyCounter timer = new MyCounter(10000,1000);
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
System.out.println("Timer Completed.");
time.setText("Timer Completed.");
}
#Override
public void onTick(long millisUntilFinished) {
time.setText((millisUntilFinished/1000)+"");
System.out.println("Timer : " + (millisUntilFinished/1000));
}
}
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
timer.start();
}
return false;
The timer code looks correct. Let me check some assumptions:
1) "time" is a TextView
2) You actually hook the onTouchEvent up to something.
Set a breakpoint in onTouchEvent and verify it's getting called. Also, take a look at the Log class and LogCat to verify the methods are getting called.
Related
I'm having android countdown timer which is starting automatically. I need to pause and resume it. I've seen other solutions but they didn't work. My code is:
class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
timerTimedOut = false;
}
#Override
public void onFinish() {
if(timerTimedOut){
doSTH();
} else {
doSTHElse();
}
this.start();
}
#Override
public void onTick(long millisUntilFinished)
{
timerShow.setText(Long.toString(millisUntilFinished / 1000));
}
public void stop(){
timerTimedOut = true;
this.cancel();
onFinish();
}
}
What should I do to pause and resume it?
I have been dealing with this and after trying many things, I found this solution. Here you can find a great alternative for the Android CountDownTimer class:
https://gist.github.com/bverc/1492672
You just have to create a new class named CountDownTimer2, and paste that code. Then, use it instead of the normal CountDownTimer class, for example:
CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//Whatever you want to do onTick
}
#Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
An then you just need to pass
cdt.pause();
or
cdt.resume();
Hope it helps.
this is my first question here.
I need to implement in my app six countdown timers running one after another. When first finishes, next one starts and so on. Init time of every single run depends on user input. The problem is that I need to put different code in onTick() and onFinish() methods for every single countdown timer running and, well, I'm not sure how to start a next counter after one is finished. I was thinking about calling next counter in onFinish() method of current one but I can't figure out how to do this with 6 counters.
This is my Countdown timer class:
public class Counter extends CountDownTimer
{
public Counter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished)
{
//this code is the same for every counter
timer_view.setText(formatTime(millisUntilFinished));
//this code depends on specific counter running
output_view1.setText("My output here");
output_view2.setText("My output here");
output_view3.setText("My output here");
}
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
timerHasStarted = false;
}
}
I'm starting my counter in the same activity:
if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
counter.start();
}
You probably need to break out the start timer functionallity and call it in onFinish().
public void startTimer(int counterId){
Counter counter = null;
switch(counterId){
case 0:
counter = new CounterOne(counterId,user_input1,1000);
break;
/* Counter 1-5 goes here*/
default:
break;
}
if(counter !=null ){
counter.start();
}
}
then start your next timer in onFinsh()
public abstract class Counter extends CountDownTimer
{
private int counterId;
public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
this.counterId = counterId;
}
public abstract void onTick(long millisUntilFinished);
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
startTimer(this.counterId++);
}
}
public class CounterOne extends Counter{
public void onTick(long millisUntilFinished)
{
//counter 1 logic
}
}
/* Subclass others. eg. CounterTwo etc. */
You never set
timerHasStarted
to true. It's always false, so...yeah, one timer after another. Set it to true before calling counter.start() and it should work.
I'm doing an app which will use CountDownTimer
I would like to use 1 countdowntimer for 2 count downs.
If the time finishes, then a new time starts immediately, and so on.
For now I have something like this:
cdt = new CounDownTimer(time,1000) {
public void onTick(…) {
//code
}
public void onFinish() {
//and here I'm thinking to add a new time, but it doesn't work
}
};
How to do that? Or maybe is there other easier option to solve that problem?
Thanks for help!
Somewhere in your class newMyCountdownTimer(time, interval).start();
private class MyCountdownTimer extends CountDownTimer
{
public MyCountdownTimer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish()
{
if (somecondition is satisfied) // be carefull otherwise the countdown keeps running
{
// newTime and newInterval are variables in the outer class
new MyCountdownTimer(newTime, newInterval).start();
}
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
}
I trying to extend ConuntDownTimer to add the methods pause and resume in this way:
public class CountDown extends CountDownTimer {
private long resume;
private long millisInFuture;
private long countDownInterval;
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture,countDownInterval);
resume = millisInFuture;
this.millisInFuture = millisInFuture;
this.countDownInterval = countDownInterval;
}
public void play() {
// start
if( millisInFuture == resume ) {
super.start();
// restart
} else {
CountDown cd = new CountDown(resume, countDownInterval);
cd.play();
}
}
#Override
public void onTick(long millisUntilFinished) {
resume = millisUntilFinished;
// other code
}
}
The problem is "restart" in the play method because, in this way, I created another istance of CountDown that display the wrong seconds because there are almost two event "onTick". Could I solve this problem? (I hope my English was understandable)
I don't see any way to do what you want by extending the class. You'll be best served writing your own. Here's the source for it.
Is there a timer functionality or timer that is a subclass of View in android that can be use to keep track the playing time of the user?
If I must build one, is it good to build a Thread or Runnable class that have a loop that put itself to sleep for 1000ms (Thread.sleep(1000)) then update itself. e.g.
public class TimerThread extends Runnable{
private int time;
boolean run = true;
public void run(){
while(run){
//update the View here
Thread.sleep(1000);
time++;
}
}
}
This might be helpful.
http://developer.android.com/reference/android/widget/Chronometer.html
I think you might need to Google for an example never really used it myself but I've heard of it.
Here is some code I use in one of my games. Hope it helps
MyCount counter = new MyCount(30000,10); //set to 30 seconds
counter.start();
public class MyCount extends CountDownTimer{
long time=0;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
clock.setText("You lose!"); //clock is a textfield in the game
restart(); //call you own restart method
}
#Override
public void onTick(long millisUntilFinished) {
DecimalFormat df=new DecimalFormat("#.##"); //import java.text.DecimalFormat;
clock.setText("Left: "+ df.format(millisUntilFinished/1000.0));
time=millisUntilFinished;
}
public long getTime(){
return time;
}
}
Take look at.
http://developer.android.com/reference/android/os/CountDownTimer.html
Just to provide more options, here is another approach: Updating the UI from a Timer