Let's say I have this variable:
long myMillis = 20000;
This means that I want my Chronometer to start at exactly 20 seconds (00:20).
I tried doing this:
chronometer.setBase(myMillis);
But it doesn't work. It dosn't start with 20 seconds. It starts with some weird time that doesn't make sense.
In general the chronometer works like this (if you would like to set
the Base to a specific nr):
mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))
so make it:
mChronometer.setBase(SystemClock.elapsedRealtime() - (2* 60000 + 0 * 1000)))
For Kotlin,
To start Chronometer with starting time 20 seconds, you can use
val timeInMilSeconds = 20000
chronometer.base = SystemClock.elapsedRealtime() - timeInMilSeconds
chronometer.start()
This will start Chronometer with starting time 20 seconds i.e. 00:00:20
Its Late but may help others.
I have used following code in first fragment
chronometerTimer.setBase(SystemClock.elapsedRealtime());
chronometerTimer.start();
and then move on some condition to next fragment where chornometer should start at same time of previous chornometer ends, i get elapsed time using this code.
long elapsedMillis = SystemClock.elapsedRealtime() - chronometerTimer.getBase();
and i send elapsedMilis in next fragment and use following code
chronometerTimer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
chronometerTimer.start();
it worked perfectly.
Related
Each second that != the previous second a new value is posted and when the value reaches <1 the countdown finishes.
I note the starting time with System.currentTimeMillis() and simply calculate the remainder of the countdown from there. All this is done in a runnable which gets re-run over and over.
When typing out the min & seconds left I use this formula:
secondsLeft= (int) ((time / 1000) % 60);
minutesLeft = (int) (time / (60*1000));
The problem Im getting at is that when the secondsLeft reaches <1 the timer finishes. But my Alarm set through AlarmManager which uses pure millis and thus not rounding to nearest second, gets run a little sooner , or later than the timer finishes.
What can I do to make them synchronized?
Some extra info:
I am vibrating the phone when the countdown finishes. Hence I cant have the timer reach 0 and then the phone vibrating sooner/later.
I use AlarmManager for the vibrate as it wont go off if phone is asleep too long (service running in foreground works most of the time,but not 100%).
By your description, I believe you're getting bit by rounding. If you start the timer at, say, 1380866264454ms, next second by your algorithm happens in only 546 milliseconds, not 1000 - so your countdown would end approximately 454ms before your alarm is scheduled. Thus, get the last three digits of your start time and subtract them from the current time before you do the calculation you show in the post. The first change after (1380866264454 - 454) will be at (1380866265454 - 454), at least 1000ms away.
Unfortunately my architecture does not work so i need your help.
The Problem which i want to solve:
I programmed a mini game in which you can select exercises like "run for 1 minute".
showing countdown anywhere in app: fragment, activity, actionbar...
do task after finish
countdown should run ahead if leaving the fragment
countdown should with the right progress again if i go back to the fragment
I tried to solve the Problem while implementing CountDownTimer. But if the countDownTimer is part of the fragment it will be destroyed if i leave the fragment. When i come back the countdowntimer begins from the beginning...
So i thought about a thread... but how can i display the countdown in the fragment and anywhereelse i want?
Will the thread alive until he is done? Even if i close the application?
So can you name please some keywords i can search for to solve the problem. Or instead describing a solution?
Thanks for your advices ;)
You should probably put your counter in a Service which will run as a remote process and bind to that service everywhere you need to display the timer, then use the observer pattern to call a listener callback from you service CountDownTimer's onTick method whenever you you want to update the observers:
mCountDownTimer = new CountDownTimer(startTime, 1000) {
//update after each tick
public void onTick(long millisUntilFinished) {
long seconds = (millisUntilFinished / 1000) % 60;
long minutes = ((millisUntilFinished / 1000) / 60) % 60;
long hours = (((millisUntilFinished / 1000) / 60) / 60) % 24;
long days = (((millisUntilFinished / 1000) / 60) / 60) / 24;
if(listeners!=null){
Log.d(TAG, "days:"+days + "hours:"+ hours + "minutes:"+ minutes + "seconds:"+ seconds);
listeners.onTimeChanged(days, hours, minutes, seconds);
}
}
public void onFinish() {
if(listeners!=null){
Log.d(TAG, "timer finished");
listeners.onTimerFinish();
}
}
}.start();
I wouldn't try to make an actual countdown, because your threads should die when the appli leave and that seems really complex.
Instead, write somewhere (sqlite db ? file ?) the start time and the duration of the countdown running.
Whenever you need to display it, query it, and do the needed process to update view, whatever view is in place. That answers easily (for me) all you specifications, without the threading burden, and only uses a tiny amout of disk space. Plus you can save all your countdown specific data without any problem, which will probably prove useful.
Is there a way by which using the android chronometer class to set base of the chronometer in 15 minutes and from that period the times goes down until 0 seconds?
I have tried with setBase(60000) but this isn't work.
Check out this thread Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"? as well as this thread Android - Get time of chronometer widget. Neither answers your question directly, but the nuggets there should lead you to an answer.
In general the chronometer works like this (if you would like to set the Base to a specific nr):
mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))
what you are asking can be done through a countdown (http://developer.android.com/reference/android/os/CountDownTimer.html)
Or create your own countdown by using the chronometer like this (more work should be done cause i just wrote this and did not test it yet)
private OnChronometerTickListener countUp = new OnChronometerTickListener(){
#Override
public void onChronometerTick(Chronometer chronometer){
long elapsedTime = (SystemClock.elapsedRealtime() - mChronometerCountUp.getBase()) / 60000;
Log.v("counting up", elapsedTime);
// you will see the time counting up
count_down--;
if(count_down == 0){
mChronometerCountUp.stop();
}
// an int which will count down,
// this is not (very) accurate due to the fact that u r using the update part of the chronometer
// u just might implement the countdown i guess
// or 2 chronometers (one counting up and an other counting down using the elapsed time :p)
// just remember programming is creating ur solution to problems u face its like expression urself
};
};
http://developer.android.com/reference/android/widget/Chronometer.html
For set the base time you can use elapsedRealtime(), and you can output format with setFormat()
im working on a audio profile switcher for android and as part of the entire project, i have a service that is running in the background using the following timer code:
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {.....}, 0, nextUpdateInterval);
what im noticing is that the timer is not honoring the dynamically generated next update interval period...the nextUpdateInterval is declared as private static long which is initialized to 30000 (30 seconds) for the first run....then once a profile is found, i do some math and update the nextUpdateInterval...i have converted the nextUpdateInterval value back out to hours/minutes for debugging purpose, and the calculation is working as expected...like it shows me in hours and minutes, when the next timer execution should take place...
nextUpdateInterval calculation: long entirePeriodDiff = toTimeMiliseconds - fromTimeMiliseconds;
then once a profile is found, i calculate the elapsedTime like so: long elapsedTime = rightNowDate.getTime() - fromDate.getTime();
and then i update the nextUpdateInterval: nextUpdateInterval = entirePeriodDiff - elapsedTime;
one example scenario: Profile of 'Work' is set from 9AM to 4:30PM, the service/app is executed at 2:02PM (EST), my toast message is executing constantly and is acting as a count down telling me how much time is left...in this case 2:28 and decreasing...ideally this should not display until the 2:28 is up...any ideas?
As per android doc:
With fixed-rate execution, the start time of each successive run of a task is scheduled without regard for when the previous run took place. This may result in a series of bunched-up runs (one launched immediately after another) if delays prevent the timer from starting tasks on time.
I think that could be the reason, may be you need to consider alternative 'fixed period'
How to setup service to something (api request) everyday at particular time.
I dont know.
Right now I thing about two options:
1. Setup timer and every hour check the time and if it right, do a request.
2. setup the alarm, by alarmManager, but I dont know how to do it.
Another imported thing is the request must be a little random.
About 3-10 minutes, to prevent blocking the server by too many
request at the same time.
Take a look at this tutorial for scheduling events with an AlarmManager.
For the interval of 3-10 minutes you could just add something like
int rand = (int) (Math.random() * 1000 * 60 * 7 + 3 * 60 * 1000);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + rand, sender);