in my current android project i'm trying to calculate an activities elapsed time in the foreground. I've found few functions in this site.
Are these built in functions which just returns the time or should i have to define these methods in some other class?
Someone please help.
You can take time of your activity in foreground in a hack simple way without any libraries.... Just #Override OnPause() - where you get the current time, and then when application returns from foreground #Override OnResume() where you get the current time too... Then just calculate the difference!!)) You can save cur.times in sharedPrefs for example, and in OnDestroy() set them in null!!)
Related
I am trying to write to a file whenever the user has not interacted with the application for 2 minutes. Currently am having base activity in which I have overriden the onUserInteraction method. In this method the float time variable is reset and onResume I subtract the time with the current time to check if two minutes have passed. This works fine but sometimes acts crazy. Second approach was using the postDelayed method of the Handler and start a thread. This works perfectly but does not include the case when the app goes to background or the device goes to sleep.Is there a way to cover all these cases. ahve researched a lot. Also came across Wakeful Intent service but read that it is expensive.
Is there a way to cover all these cases.
Yes. ... and it is expensive. Waking the phone up, every 2 minutes is going to drain the battery like crazy.
That said, the answer to your question is that you need to use the AlarmManager, probably in concert with either the WakefulIntentService or the WakefulBroadcastReceiver. Create a PendingIntent and schedule it for delivery every 2 minutes.
I don't know a way of tracking inactivity but there is a way to track user activity. You can catch a callback called onUserInteraction() in your activities that is called every time the user does any interaction with the application. I'd suggest doing something like this:
#Override
public void onUserInteraction(){
MyTimerClass.getInstance().resetTimer();
}
If your app contains several activities, why not put this method in an abstract super class (extending Activity) and then have all you activities extending it.
I need to calculate the time taken by my app to start. Do you have any ideas when to start and when to stop timing?
Use your timer at the beginning of oncreate() and stop it at the end of onResume().
According to lifecycle of an Activity.
To do this, you probably should be clear on what "start my app" means - if you are referring to an activity, then you should probably override the Activity constructor (not "onCreate" except in most cases there isn't any measurable time from the constructor before onCreate is called) and capture:
SystemClock.upTimeMillis()
then you need to create a listener for onGlobalLayout to determine when the activity is actually finished displaying on the screen and get the upTime again. For the listener, do it like in this post:
Activity lifecycle - receiving notification that layout is complete
And then take the difference between the times... however, that is really the "activity" startup time.
If you are certain your app is not running, and you want to see how long it takes to "start your app" when it is not loaded in memory at all, you should extend the Application class:
public class MyApp extends Application {
public MyApp() {
// get the time
}
}
and then override the constructor to capture the timestamp like above and store it. Your app's application class is constructed first, then activities are instantiated.
Then capture the listener time in the first activity to display, as mentioned above. That will probably tell you best how much time it took from when your app "starts" to when the user actually could see it.
There are a few things that happen between when a user action occurs that is intended to "start your app" that are not included, but you have no control over that, even though that should be included if you are trying to measure how long it takes from the user's perspective.
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 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 have basic question regarding when to unregisterListener for sensor manager. Should it be done in onStop() or onDestroy().
Usecase :
I want to record accelorometer on click of start button and stop when user clicks stop button. the frequency of data is every minute. So I have started a timer.
But the issue is every time the orientation changes as per Android architecture, onStop() gets called. In onStop() I am cancelling the timer and unregistering the listener.
SO again if I start timer/register listener in onResume() the frequency won't remain 1 minute and also the data gets recorded without user pressing the start button.
Can someone help me resolve this issue.
Thanks.
You may want to use a Service to run the accelerometer data collecting in the background, communicating with it using basic intents is not too complicated.
Or, if you only want to 'survive' the rotation, when the activity gets destoryed and then rebuilt, try overriding in your Activity the onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods (read more about it here)
OnDestroy() isn't guaranteed to be called. So you should do it in either onPause or onStop
But the issue is every time the orientation changes as per Android architecture
There's a way to prevent this from happening in the manifest. I'm sure someone else will post how.