i want to save the time when countDownTimer begin working, is there any method like onStart()?
timeToDirectAnswer = new CountDownTimer(25000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
};
The timer starts ticking the moment start() is called on it. You can get the current time wherever start() is called.
Related
Here my code that uses the CountDownTimer class:
private void initCountDownTimer() {
countDownTimer = new CountDownTimer(millisInFuture, COUNT_DOWN_INTERVALE_MS) {
#Override
public void onTick(long millisUntilFinished) {
getView().setCountDownTimerValue(millisUntilFinished);
}
#Override
public void onFinish() {
getView().setCountDownTimerValue(0);
}
};
}
It works fine.
But the question is: Is it possible change millisInFuture without recreate new object CountDownTimer?
No there is no way to change millisInFuture, without re-initializing the CountDownTimer using the constructor, since it is not an exposed variable. You can cancel() the timer and start a new one
I have an Android service running in the background.
I want to be notified after a specific period of time (22 seconds), so I wrote:
private CountDownTimer mCountDownTimer = new CountDownTimer(22*1000,22*1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
doSomething();
}
};
I run this, and get the notification after 40 seconds, and even 50 seconds. Am I doing something wrong? How can this be done?
Actually you are putting interval time as wll 22000, what this you are doing wrong. Second parameter is the interval.So , Do this :
CountDownTimer alertTimer = new CountDownTimer(22*1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do here what you want
}
#Override
public void onFinish() {
}
}.start();
I have an Activity. All I want is if user don't touch screen, after a TIMEOUT, this activity will automatically finish(). Anyone have any ideas?
CountDownTimer countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//TODO: Do something every second
}
public void onFinish() {
finish();
//YourActivity.finish(); outside the actvitiy
}
}.start();
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
countDownTimer.cancel();
countDownTimer.start();
}
return super.onTouchEvent(event);
}
So basically you start a countDown timer and than wait for it to get completed. Once completed, call finish on the activity.
If in between the user touches the screen, you cancel the timer and start it again.
Don't forget to accept the answer :)
Look for how to wait/sleep in Android, examples:
wait for 3 seconds or user click
Wait a time - Android
How to "wait" a Thread in Android
And when the wait/sleep ends just call the finish() method.
Try this in your Activity:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
//YourActivity.finish(); outside the actvitiy
}
}.start();
http://developer.android.com/reference/android/os/CountDownTimer.html
use this :
int time =0;
then add onTouchListener for your root layout and update time value like this :
time = 0;
and use the handler for count like this :
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
time++;
if(time>=10000){
finish();
}
}
}, 1000);
it is clear i set finish time for 10 second.
my CountDownTimer won't stop, i call the cancel() method but nothing happens, it continues to work. I hope you can help me, i've searched something around the web and all say that cancel() method works, so where have i made a mistake?
cd=new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if(isNetworkAvailable() == true) {
countdown.dismiss();
chiudi=false;
timerCount.start();
cd.cancel();
}
countdown.setMessage("00:"+ (millisUntilFinished/1000));
System.out.println("ancora");
}
#Override
public void onFinish() {
countdown.dismiss();
finish();
}
}.start();
I'm creating an app that connect with server(localhost).
In my Activity, I have a countdown timer which will tick for 30 seconds and at the finish() I have some code to check if there's any changes in the database on server, and then start the timer again so it'll be a loop in the activity. But the timer seem like running just once. When I'm running in debug mode, the timer get some error at the finish(), it said that I'm missing some folder or file that related to the API or the SDK version which is being run in android:targetSdkVersion="9"
Please help me with this issue. Here's the activity:
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
start();
}
};
counter.start();
}
}
Do I have to make a thread or not?
public class Account_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Creating the UI and some methods
counter = new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
//Codes for checking the changes in the database on the server
new CountDownTimer(30000, 1000).start(); <------Start Again.
}
};
counter.start();
}
}