I am a beginner in Android. I have created a chronometer and it works. I added a new function (every 5 minutes a value 1000 shown in the textview, after 10 minutes it takes 2000). This function works but if I click a button exit and I re-open the chronometer, after 5 minutes the value continues to add to the previous value but I want to reset this value once I click exit. I have used
finish(); handler.removeCallbacks(null); handler.postDelayed(test, 100); but it doesn't seem to work. Thanks.
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (lastPause != 0) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
} else {
chronometer.setBase(SystemClock.elapsedRealtime());
}
chronometer.start();
suppl();
btnStart.setEnabled(false);
btnStop.setEnabled(true);
btnExit.setEnabled(false);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
onPause();
lastPause = SystemClock.elapsedRealtime();
btnStop.setEnabled(false);
btnStart.setEnabled(false);
btnExit.setEnabled(true);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
handler.removeCallbacks(null);
handler.postDelayed(test, 100);
}
});
}
Handler handler = new Handler();
Runnable test;
public void suppl() {
test = new Runnable() {
#Override
public void run() {
count += 1000;
tprice.setText(String.valueOf(count));
handler.postDelayed(test, 5000);
}
};
handler.postDelayed(test, 5000);
}
public void onPause() {
super.onPause();
if (handler != null)
handler.removeCallbacks(test);
}
I would suggest using a TimerTask for this purpose.
While using time task you can cancel all the pending tasks by using cancel() on the timer.
if you have any doubts in this solution, let me know.
Instead use CountDownTimer
CountDownTimer CD = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
xo = (int) (millisUntilFinished / 1000);
secondsleftimg.setText("seconds remaining: " + millisUntilFinished / 1000);
Try this out :
Replace this line of code :
handler.removeCallbacks(null);
with this :
handler.removeCallbacksAndMessages(null); // it will remove all hadler
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
focus.setBase(SystemClock.elapsedRealtime());
handler.removeCallbacksAndMessages(null);
lastPause= 0;
}
});
Related
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();
}
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();
}
I'm attempting to do a guessing game, of sorts. The issue is my timer bleeds into the next one after a question is answered (button pressed) and a new timer starts. This leads to two timers changing a textview at different intervals, which is not how it's supposed to be. I'd like to know how to stop my previous countdown and start a new one. Thanks! Here's my code:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView textic = (TextView) findViewById(R.id.button1);
long total = 30000;
final CountDownTimer Count = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("OUT OF TIME!");
finish();
}
};
Count.start();
Heven't tested the code but I would use something like this:
final TextView textic = (TextView) findViewById(R.id.button1);
final android.os.CountDownTimer Count = new android.os.CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
textic.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
textic.setText("OUT OF TIME!");
}
};
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Count.cancel();
Count.start();
}
});
finish CountDownTimer in On finish
#Override
public void onFinish() {
Log.v(TAG, "On Finish");
textvieew.setText("your text");
countDownTimer.cancel();
}
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();
}
i am developing a game.i want to display score on button click.but it should be displayed only for few seconds.i want to implement timer in my app.but i dnt knw how to implement that.i searched in the google.but results were confusing me...given below is my code snippet.plz anybody help me...
OnClickListener clickball=new OnClickListener() {
#Override
public void onClick(View v) {
score=scorenumber.nextInt(9);
id=v.getId();
Log.v("", "u clicked me");
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
}
else if(id==R.id.ball3)
{
ball3.setText(Integer.toString(score));
}
else if(id==R.id.ball5)
{
ball5.setText(Integer.toString(score));
}
}
}
Something like
//Show score here
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//hide score here
}
}, 2000);
will hide your score after two seconds.
No need to go so deep in your case. Every view has postDelayed() method, that will run a custom code in UI thread after a set amount of time (in milliseconds). For example:
ball5.postDelayed(new Runnable(){
#Override
public void run() {
ball5.setText("");
}
}, 3000);
will clear "ball5" text after 3 seconds has passed
Another example
...
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
ball2.postDelayed(new Runnable(){
#Override
public void run() {
ball2.setText("");
}
}, 3000);
}
...
you can use CountDownTimer(); refer the documentation ,
Example :
new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
//hide your score here after 5 secondes (5000/1000)
}
}.start();
I think CountDownTimer is that what you need:http://developer.android.com/reference/android/os/CountDownTimer.html . It's quite simle for implementation
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();