How would I calculate the total time an activity has been displayed on the screen? It should be the total time it had been on screen, across its various launches, since it has been installed.
Keep a static time variable. Start a new timer every time your activity is on screen, that is when onResume is called. And suspend the timer when your activity goes out of screen, that is onPause, onStop, onDestroy and add the time elapsed to the time variable.
Copa's answer will be very useful to count the time through many sessions of your application.
Use the activity lifecircle callback functions to detect what happend to your activity:
http://developer.android.com/reference/android/app/Activity.html
The information how long an activity lived can be stored using the SharedPreferences:
http://developer.android.com/reference/android/content/SharedPreferences.html
Related
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!
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()
I need to start the log in Activity if the user wasn't active for 5 minutes in the application
,without considering from what activity he left the application. (by not active for 5 minutes I mean that the user didn't commit any action to the server side)
I have a Date variable inside my Application class:
private Date timeOfLogin;
that's is saved when the user commits log in, in some point of usage the user can get a phone call or
a mail and will leave the application. now this can happen on any screen of the application. And now when he turns on the application again /return to it after finishing his phone call I need to show the log in screen again and not his last activity if 5 or more minutes have passed.
How can it be done? do I have to override every onResume of each activity I have in the application and start the log in Activity if the difference between the timeOfLogin and current date is bigger then 5 minutes? or is there a better way to do that?
Any help would be appreciated.
Thanks.
Have an activity that all of your other activities extend and put the logic in that activity's onResume. (You'll probably find this practice to be useful in a lot of other ways too)
Create a CountDownTimer with 5 minute count down in your ResponseHandler class in the onFinish() show the login dialog. If there is any response cancel the timer and start again.
I am new to android.
I am implementing one application.
Related to my app
I want to get starting and stoping time of each application.
If any one know the solution please help me,
Thanks in advance.
If you are not stopping your activity by using finish() method.. then i think it is difficult to know when the activity is closed exactly.. because onDestroy is the method called when the activity is finished (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space... but there are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it...See about onDestroy method here
you can get starting time onStart() function and activity end time in onStop() just define given code in the functions.
Calendar c = Calendar.getInstance();
Date StartTime = c.getTime();
Log.i("Start Time", String.valueOf(StartTime));
First take a shared pref.
As you know your application when it first launches in that component(activity,service) go to oncreate() and create share pref and save the current system time.
and finally u know where is last activity or service in that ondestroy(). bring the values which u stored in shared pref.
Now u minus current system - this stored start time. you will get the time total time spent in you app. if you want u can save this to shared pref.
I am calculating creating apps for Online Shopping in which i am having a Deals Start Date and End Date..
From this two date i want to calculate the time remaining for the Deal to end in Seconds basis.. I calculated remaining days,seconds,hours,minutes..
I want to display the Remaining time in Days:Hours:Minutes:Seconds format...
Although i move to next activity this time should runs in background in which the second must updated every seconds..
If i move this activity again i want to display the Updated Remaining time...
Also in my UI the Remaining time must get reduced second by second.
Instead of building a service I would create a Handler that updates your Userinterface.
You can now call sendEmptyMessageDelayed with a delay of 1000ms. In the handleMessage method you can update your UI and call sendEmptyMessageDelayed again.
Don't count on Android to call you exactly in time. recalculate the remaining time every now and then instead of just decreasing it by one.
If the activity is in the background you shouldn't update the UI because the activity is paused. Just disable the whole updating process in the onPause method and reenable it in your onResume method. If you method is destroyed from the system while it was in the background your onCreate method will be called again and you have to recalculate the actual remaining time.
Make sure you understand the ActivityLifecycle Process before implementing this changes.
Sounds like you need a Service and an Activity with the Service performing the timing operation and the Activity presenting the time information.