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();
Related
I developed a simple android application and was able to post and get information from database through http request and doing this with AsyncTask. My question is if i want to poll information from the db every 5 mins or check if any new data has been posted to the db how i could this be done in android?
Use CountDownTimer
check this out
300000ms = 5 min
CountDownTimer chkDb= new CountDownTimer(300000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
//code for db check
chkDb.start();//start again after 5 min
}
}.start();
OR
Use TimerTask
private TimerTask mTimerTask;
private Timer t = new Timer();
public void doTimerTask() {
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
callYourMethodHere();
Log.i("TIMER", "TimerTask run");
}
});
}
};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 500, 1000); //
}
public void stopTask() {
if (mTimerTask != null) {
Log.i("TIMER", "timer canceled");
mTimerTask.cancel();
}
}
task -- This is the task to be scheduled.
delay -- This is the delay in milliseconds before task is to be executed.
period -- This is the time in milliseconds between successive task executions.
I don't think that polling the DB every 5 min is a nice solution. It's a waste of resources.
Instead of that, consider using some mechanism that informs you when something in the DB has change so your UI can refresh properly.
For example, in some projects I have used Otto to send an event when a new item has been inserted in the DB. My fragments subscribe to a default bus so they are aware when something has change.
Pros: Performance. The app only works when it has to
Cons: Well, not much really. The easy solution is to say: "OK, something has change. Let's throw all the current info shown in UI and make a new query to DB!". BUT the nice solution is to say: "OK, something has change. What has change and how can I add that new info to UI?"
But that depends on the app context ;)
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 want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:
This is in the click listener of the Button:
new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
There are 2 issues:
The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.
The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).
Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).
To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.
For issue 2, you can simply round the number.
For example:
new CountDownTimer(30000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
performTick(millisUntilFinished);
}
#Override
public void onFinish()
{
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
performTick(30000);
void performTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}
If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.
It is skipping because CountDownTimer is not a precise timer. Try to print just the millisUntilFinished without dividing it you'll see some excess number that it wont be exactly 29000 when it hits its first tick.
you can refer on this thread for the solution.
Is there a way to continuously loop through a countdown timer? I have a basic timer of going though 60 seconds, then updating a text field and it's working, but I want to add the functionality: when it's counted down, to automatically restart again until the user cancels it? Maybe run it through a thread? Not sure how to handle it. Here is what I have, and again, this code works, but I can only stop and start the countdown timer, not do a continuous loop:
cdt = new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
tvTimer.setText("remaining : " + millisUntilFinished/1000 + " secs");
}
public void onFinish() {
tvTimer.setText("");
bell.start();
}
};
/***************On Click/Touch Listeners*******************/
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tvTimer.setText("");
btnStart.setText("Start Timer");
SetImageView2(myDbHelper);
cdt.cancel();
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!TimerTicking){
cdt.start();
btnStart.setText("Stop Timer");
}
else {
tvTimer.setText("");
cdt.cancel();
btnStart.setText("Start Timer");
}
}
});
One very basic way to loop a CountDownTimer is to call start() in onFinished().
public void onFinish() {
...
start(); // Start this timer over
}
(Make sure you cancel your CountDownTimer in onPause() when you do this otherwise the timer might leak and keep firing in the background... Oops.)
However CountDownTimers has fundamental flaws (in my opinion): it often skips the last call to onTick() and it gains a few milliseconds each time onTick() is called... :( I re-wrote CountDownTimer in a previous question to be more accurate and call every tick.
The first value to the CountDownTimer() constructor, the total time to run, is a long and can hold a value approaching 300 million years. That ought to be long enough for most mobile applications. :-)
So just call it with cdt = new CountDownTimer(Long.MAX_VALUE, 1000) for a one second loop that will run continously.
I am working on a mini game for Android. I am using this code to create a 3 minute timer that pauses when the game is paused:
class update extends Thread{
#Override
public final void interrupt(){
super.interrupt();
}
#Override
public void run(){
try{
while(true){
sleep(50);
iTime++;
if(iTime>=3600)
bEnd = true; //finish
postInvalidate();
}
}catch(InterruptedException e){
}
}
}
update thread = null;
3600 = 20*3*60
The timer finishes in the emulator within about 5 minutes, and in the Galaxy Tab after about 1.5 minutes.
Why doesn't it always take 3 minutes? How do I ensure that it finishes within 3 minutes?
How about using the Android CountDownTimer class?
// 30 second countdown
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I am not sure what are you doing in the postInvalidate() but maybe you can do something with AlarmManager and a service. You could create a task that will run in 3 minutes from now, and it will execute the service which, in your case, will invalidate the game.
I've seen many questions that were trying to handle timeouts with sleep method and I believe Android doesn't guaranteed that it will work that way.
Post back if you need help creating the service or running something with AlarmManager. I answered a question about it a couple of days ago.