How to keep a Chronometer running in the background? - android

I have in a Activity (1) A Chronometer and is running fine. I click a Button and starts Chronometer time count.
I'm trying to keep the score Chronometer changing Activity (2) but without success.
In this new Activity (2) does not have the layout Chronometer one component.
I tried to pass the Chronometer object of Activity (1) to the other Activity (2) and continue the count but continues even taking this Chronometer and giving a new start() does not count the time.
I tried to pass the component Chronometer via Intent.putExtra() and creating a memory class.
I tried to even create the Activity (2) a Chronometer in invisible layout and pass only the value of Chronometer.getBase() and give a start(), is not counting the time.
In Activity (2) should continue recording the time and return to the Activity (1) Chronometer should be visible elapsed time.
The Activity (1) and (2) die with the finish().

You just need to keep a reference to the original start time. See the Chronometer docs for more details, but the gist is:
long startTime = SystemClock.elapsedRealtime();
chronometerInstance.setBase(startTime);
chronometerInstance.start();
Then you can pass startTime around to other activities and continue to use that same value anytime you need to start a chronometer.

Thanks for the reply! I had tried before this medium but without success. In fact I analyzed my code better and saw that I was sabotaging myself. A new instance of Chronometer was created after setting it to start counting. So it does not accounted for. and taking advantage would have to use their means of more this code timing:
long restartTime = getIntent().getLongExtra(Constantes.TIME, 0) + SystemClock.elapsedRealtime();
This was when set the current getBase the Chronometer in question the previous Activity.
And step for the Activity to be called the following putExtra():
long valueChrono = chronometer.getBase()
- SystemClock.elapsedRealtime();
Thank you and sorry for any problems!

Related

How to implement countdown timer [re-open activity]?

How i should implement a countdown timer if i need the following usage:
User starts activity A;
In activity A he starts the timer;
User leaves activity A, timer runs in background;
User comes back to activity A;
User see current (updated) values of the timer;
I tried to use a Countdown Timer but it fails at step 5 - after returning to ativity A i cant see values in TextView (in LogCat i do) and system starts another timer (this way, every activity re-open starts one more timer). Should I use service instead or ... show me the way, please.
You can't be sure that after you leave your activity it will be still alive. System could kill it at any time.
The solutions for this use case could be:
Start timer in foreground Service. Such types of services have better chances to be alive.
When you leave your activity you could save last timer value and last timestamp and restore timer with this info after activity is recreated.
PS. If you also want to fire the alarm when timer is finished you should use AlarmManager but keep in mind restrictions from android 6+ (M).

How to save time from the chronometer and access it from another activity

The goal is to save the time shown on the Chronometer. I want to press stop and have the value stored in a variable so I can call it in another activity.
So far I can store the time on the Chronometer to a variable with the function showTime, the time I want to push to another activity is stored in elapsedSecs.
public void showTime() {
Chronometer chronometerLeft = (Chronometer) findViewById(R.id.chronometerLeft);
long elapsedMillis = SystemClock.elapsedRealtime() - chronometerLeft.getBase();
double elapsedSecs = elapsedMillis / 1000;
}
My issue is when I try to call showTime in the other activity I get the error that the showTime function needs to be static and when I make it static I can't use findViewById.
Goal:
The end goal is to be able to press stop on my ChronometerActivity and then from my DataActivity be able to press a button to collect the data and the time to pop up.
i.e.
Before data collect:
Time Elapsed:
After data collect:
Time Elapsed: 45 seconds
I can provide more code with regards to my project if necessary. Thanks!
You read it in the chronometer activity and send it to the second activity via Intent. You cannot share a view between activities or access it directly. Nor can you call a method of another activity directly- you don't have a copy of that activity to call it on.

How to create a timer runed in the background and display one second of a second dynamically

How to create a timer runed in the background and display one second of a second in the TextView dynamically in MainActivity.Whatever I exit app or leaving the MainActivity,the timer also keep on.
I thinked that I can use service and BroadcastReceiver or using Handler and Thread.But I can't solve it.
Having an always-on service to simply count up seconds is a bad idea, it'll waste battery life and could be killed at any time.
What you could do is something like this:
Make a counter that you can start/stop with a button.
On your your activity's onPause, save the System.currentTimeInMillis() along with the current count on your timer
When you resume your activity, use the current System.currentTimeInMillis() value to calculate what your timer should be displaying had it been really running all that time.
I have thinked a solution; Thread+Handler; EveryTime enter ther MainActivity
we just set the time interval = EndTime-System.currentTimeMillis();

What does System.nanoTime() return in Android?

I have a bunch of activities tied together, one into the next and so on. Now during one activity I want to measure elapsed time. As I understand, I would use System.nanoTime() to find the start time, the user does some things, then use it once more to find the end time, subtract the two and voila my elapsed time spent on the activity. But suppose something happens while my activity is running: I already have created the start time, but now the user gets a phone call or something, my activity is put into the background. The phone call ends and the user returns to the activity. Was the timer running the whole time, even while the app was in the background? Is the timer 'reset' since I left the app then came back to it?
Also, when I do initiate System.nanoTime() is it returning the time since the start of that particular activity or the main activity?
EDIT: Suppose I set the first tickmark at a certain point, then the app goes into the background, then it returns to the foreground and I set the second tickmark. Ideally I want the time elapsed along with the time spent in the background; does System.nanoTime() achieve this?
static long nanoTime():
Returns the current timestamp of the most precise timer available on the local system.
You aren't using a "Timer" (that is, a stateful object of any kind) to represent the elapsed time, you are just hanging on to a long. As you pointed out, you will call System.nanoTime() again at some future point and subtract to find the elapsed time.
If you want to exclude time spent outside of this activity, like the example in your question, you will need to use onPause() and onResume() to help you manage the calculations. Or, if you switch to some kind of timer object, use these methods to pause and resume the timer.
You can "start" your "timer" wherever you think makes the most sense. If it's when the user initiates some action (like a button press), od it in an OnClickListener. If it's just to measure how long some method/code path runs, do it at the beginning of that.
according to the doc
System.nanoTime() returns the current value of the most precise
available system timer, in nanoseconds.
So it is not an timer. It just returns the system time in nano seconds. It has not relation with activity.
If you want to measure the lifetime of activity then get the time in onCreate and onDestroy. And if you want the time to know how much time the activity was in foreground then get the time in onResume and onPause.
You will need to override onPause() and onResume() methods in the Activity class so you can pause your timer in pause. and resume in onResume.
You should put System.nanoTime() on onResume()

How to make a countdown timer run in background in android?

I am revoking a timer in android using intent now if i want to put that timer in a background when user clicks on directly back button then how do i do that? and then when i see background apps running it should be working and can be brought to front.
The problem is that if you define the counter in your Activity, the counter is bound to the Activity life-cycle. In order to workaround this, you can create your counter in a Service and just visualize the value in Activity.
There are also several techniques which you can use when defining the counter inside the Activity. Using SystemClock to track the time in your Activity in onStart() and onStop() for example, you can manually calculate the difference between those two values and adjust accordingly your counter.

Categories

Resources