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.
Related
I'm beginner in Android and it might be really basic question.
I'm trying to create a CountDownTimer which keeps countdown between two activities. Users can go to the Activity B and go back to Activity A from Activity B.
And I am thinking of saving the remaining time in SharedPreferences. What I want to know is when the user does OnBackPressed and save the current remaining time into SharedPreferences and goes back to the previous activity, how can I restart the countdown timer from the remaining time in previous activity?
I think this method should help you:
#Override
public void onResume() { // This will be trigger when your activity is created or come to front
// Load preference
// Start timer
}
#Override
public void onStop() { // This will be triggered when your activity goes behind or before your activity destroyed.
// Cancel timer
// Save preference
}
It's really simple, just follow the comments. Let me know if this helps
To get this, you must have a separate thread to calculate count down stuff and it will be running all way weather you are at on Activity 1 or another activity. And that thread should update the activity(Check if activity is visible). To update the activity there can be various way.
You can use thread and update the activity via broadcast
You can use thread and check for the activity visibility, if that is visible do the stuff accordingly.
As you are already using shared preference, you can achieve the same thing via shared preference as well. You can use thread/service and update the shared preference on every tick or every second. And when you get onResume call back of the activity. get the value back from shared preference and use it accordingly.
Hope this will help you.
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()
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!!)
I need to know when an application finishes to stop all the local services that it starts.
How can I do it?
You could try using onDestroy() (not recommended, though) to know when Android is cleaning your app from the memory. or can also use onStop() to know when the Activity is being sent in the background.
Do implement these two methods in the first activity of the Activity stack.
Lets see if it helps, since I also haven't tried it so far.
One idea is to use BoundService despite the fact that it's your app own service. By definition Bound Service stops when the las connection is dropped.
Another idea would be to define count in Application object (you can override global Application object). You can increment the count in each onResume and in decrement in each onPause.
You could use try / finally on the main function and do the service clean up in the final block
You could use object finalizer to issue shutdown commands. You wont know exactly when it happens since the Garbage collector will run at its own pace, but you do know it will happen eventually.
I am now solving the same problem, I have an Idea and will implement it:
1.static variable called (String currentShownActivity).
2. in every Activity (onResume() ) I fill the variable with activity name). So when I navigate between the activities the currentShownActivity variable changed and carry the current activity name.
3. on every onPause(), set flag that indcate the activity onPause() called (i.e boolean IsPauseCalled)
4.in onStop () check IsPauseCalled, and the activity name.
if the activity onPause called and the name in currentShownActivity not changed (that mean we leave the activity to "no activity") and that mean [home] key pressed.
the integration way to know the the application is not running Check onDestroy of the main activity.
I would like to run some action when all activities of certain type X are closed. The first idea is to have some global counter, that is decremented each time activity X is closed, and once the counter is 0, run some action. Assuming that process can be killed, the counter have to be persisted. But one more issue remains - imagine that activity X can crash (due to some bug in code), and in this case I'll not decrement the counter.
Any idea how to implement it in robust way?
I am not sure what you mean by 'closed', but you should be able to use the Activity Lifecycle callbacks to accomplish what you are asking.
There is a diagram on the following page that shows how the Activity Lifecycle is implemented:
http://developer.android.com/reference/android/app/Activity.html
As you said, you can have a global counter for this.
Maybe the good place for it is in the Application class?
Or u just save you counter in a file and reset it every time you start your application (in a very first activity).
You can also try to save it in shared preferences.
But there is still the problem with crashes. I have just an idea. There is ACRA-library for crash-logging. Take a look in it, how do they catch the crashes.
So you can just write a method for decrementing your counter. And call it from onDestroy() or if a crash occurs.