Checking if CountDownTimer is running - android

Ive been looking to see a method to see if a CountDownTimer is running or not, but I cant find a way to, any help would be greatly appreciated
if (position == 0) {
mCountDown = new CountDownTimer((300 * 1000), 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("0:00");
String path = "/sdcard/Music/ZenPing.mp3";
try {
mp.reset();
mp.setDataSource(path);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name),
e.getMessage());
}
}
}.start();
}
For that how can I check if mCountDown is currently running?

Just put a boolean flag which indicate that by following code
boolean isRunning = false;
mCountDown = new CountDownTimer((300 * 1000), 1000) {
public void onTick(long millisUntilFinished) {
isRunning = true;
//rest of code
}
public void onFinish() {
isRunning= false;
//rest of code
}
}.start();

onTick is your callback for a running process, you can either set a property to track the status.
isTimerRunning =false;
After start -> make it true;
Inside OnTick -> make it true (not actually required, but a double check)
Inside OnFinish -> make it false;
use the isTimerRunning property to track the status.

Check if the CountDownTimer is Running and also if the app is running in the background.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myButton.setText("Button clicked");
countDownTimer = new CountDownTimer( 3000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//After turning the Smartphone the follow both methods do not work anymore
if (!runningBackground) {
myButton.setText("Calc: " + millisUntilFinished / 1000);
myTextView.setText("Calc: " + millisUntilFinished / 1000);
}
}
#Override
public void onFinish() {
if (!runningBackground) {
//Do something
}
mTextMessage.setText("DONE");
runningBackground = false;
running = false;
}
};
//timer started
countDownTimer.start();
running = true;
}
});
}
#Override
protected void onResume() {
super.onResume();
runningBackground = false;
}
#Override
protected void onPause() {
runningBackground = true;
super.onPause();
}

Related

Passing value to countdowntimer() in android

I have the user inputting the time in the countdown timer.
Right now, its fixed to 10sec. How can i change this to user input ?
CountDownTimer counter = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
Call this method where you take the input from the user:
public void startCountDown(long duration) {
CountDownTimer counter = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
}
You'll probably want to make your CountDownTimer variable global as well.

How to use CountDownTimer in my android application?

I'm writing a android application. I have a CountDownTimer in between my application and i want until the timer to zero, all my app run(user must be select the true Button) and when become zero and the user did'nt anything, the other action performed (the user loses).
this is CountDownTimer :
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
a.setText("" + millisUntilFinished / 1000);
/*while (millisUntilFinished / 1000 != 1) {
}*/
}
public void onFinish() {
a.setText("done!");
}
}.start();
and this is part of code:
if ((randomAndroidColor1 == rainbow[0]) {
findViewById(ans).setBackgroundColor(rainbow2[0]);
findViewById(ans).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
c3.setBackgroundColor(rainbow1[2]);
}
});
}
How to access to millisUntilFinished variable in above if condition?
Timer and if condition worked correctly but I want to combine them.
If I understood correctly , you can use an instance boolean variable.
boolean isWon = false;
private void countDown() {
isWon=false; // Reset every method call.
// Set listener once in method.
findViewById(R.id.answer).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isWon=true;
}
});
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// onTick works.
}
public void onFinish() {
if(isWon){
// Won
}else{
// Lose
}
}
}.start();
}

android - handler don't work in loop [duplicate]

I try to do app which show elements. Each element should start showing when the before element was hidden. Each element is showing 2 seconds.
Code:
public void gameStart()
{
do
{
data = random.nextInt(6) + 1;
if (data == 1)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element1.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element1.setVisibility(View.GONE);
}
}.start();
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element2.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element2.setVisibility(View.GONE);
}
}.start();
} else if (data == 3)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element3.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element3.setVisibility(View.GONE);
}
}.start();
} else if (data == 4)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element4.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element4.setVisibility(View.GONE);
}
}.start();
} else if (data == 5)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element5.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element5.setVisibility(View.GONE);
}
}.start();
} else if (data == 6)
{
CountDownTimer cdt = new CountDownTimer(2000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
element6.setVisibility(View.VISIBLE);
}
#Override
public void onFinish()
{
element6.setVisibility(View.GONE);
}
}.start();
}
id = id + 1;
text.setText("cos " + id);
} while (id < 3);
All elements are being shown in the same time. And I try add
Thread.sleep(2500);
But this stoped the action in window. I try add this after code:
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
}
}, 0, 60*1000);
But loop wasn't stay. I try do it with notify() and wait(), but it also don't work.
Don't suggest me to do do next action in onFinish() because this must be repeat a lot.
Anybody have another idea?
EDIT
I also use Handler but it don't work
I would recommend you not to implement this code in the main thread.
Create an async task, which hides a button and on postExequte calls itself.

CountDownTimer cancel method not working

I was doing a basic test on "CountDownTimer" and after a particular sec i want to stop
the countdowntimer completely. i used the method cancel but its not working the way i want
please help me out.
below is the complete code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView)findViewById(R.id.txtView);
startTimer();
}
public void startTimer()
{
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished / 1000;
display.setText("seconds remaining: " + secondsLeft );
if(secondsLeft == 10)
{
display.setText("CountDownTimer Canceled");
countDownTimer.cancel();
}
}
public void onFinish() {
display.setText("done!");
}
}.start();
}
Try following code, worked for me
if(TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)==0<put min here> && TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))==50<put your seconds here>)
{
display.setText("CountDownTimer Canceled");
countDownTimer.cancel();
}
Hope this will help

CountDownTimer issue

Basically my aim is to have this timer reset whenever the user presses button b. I've tried a few methods such as if( i==true && bIsPressed()) but no luck, any ideas?
//2 buttons
Button =b;
TextView = time;
//countdown code
CountDownTimer Count = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("Finished");
}
}; Count.start();
Haven't tested it, but I would do something along the lines of:
private void setupTimerResetButton()
{
mTimerResetButton.setOnClickListener(new OnClickListener(){
public void onClick(){
resetTimer();
}
});
}
private void resetTimer()
{
if(mTimer != null){
mTimer.cancel();
mTimer = null;
}
mTimer = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
mTimerTextView.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
mTimerTextView.setText("Finished");
}
};
mTimer.start();
}

Categories

Resources