Measuring app load times in Android - android

My App landing page has 3 async HTTP calls which gets called as soon as the app opens. What I want to know is not the time it takes to render the app (that could be done by setting up a timer on the activity lifecycle events) but the time it takes for all the HTTP calls are completed.
So if call1 takes 100ms, call2 takes 200ms and call3 takes 150ms then page loading time would be 200 ms (plus the small additional time required for rendering)
In the web world, I could use javascript promises to set up code to called when all the async calls finish. Is there something similar I can do in Android?

Well you can add a static field on your activity and make it volatile.
public static volatile int TOTAL_TIME = 0;
Then start by counting the time on each async task. You can do it by getting the current time at the beginning of doInBackground() and compare it with the time before you return the value. Then add the difference to that static field.
MyActivity.TOTAL_TIME+=difference;

You can also beside #Pedro Oliveira Solution
Instead of calculating in each doInBackground() method you can save the start time in onStart() method in the first request, and save the time in the onSuccess(), onFailure(), onRetry() in the last request then subtract the two times to get the delta time between both.

Related

Android - refresh activity

My android application has 2 activities:
In the first one (MainActivity), the user chooses some parameters and these parameters are sent to the second activity (Display).
The second activity calls a web service, and according to the chosen parameters, the web service returns a value. I use the returned value to draw a bar chart of the evolution of this value. That's why I created a timer in the second activity that I put in the onCreate() function:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
finish();
startActivity(getIntent());
}
},10000, 10000);
So every 10 seconds, the second activity is called again and the bar chart is updated with the new returned value.
The problem is, after the 2nd or the 3rd execution of the timer, several identical values are returned at the same time, as if the activity was called several times. And then the application starts freezing (but doesn't close).
I'm using the charts provided by this library: http://android-graphview.org/
I've also tried using the functions provided on the above website (resetData and appendData), and also the invalidate() function, but nothing works.
Any ideas why this happens? Is my way of refreshing the activity wrong?
The way you described it:
you create a new Timer (see 2.) with every call of the second activity
The purpose of each Timer is to call the second activity (see 1.) every 10 seconds
As a result the amount of times the second activity is called as a function of time increases exponentially.
A possible solution would be to move the timer to the onCreate method of your Main Activity (and still call the second activity from it).
This way there should be exactly one Timer active at any time.
EDIT:
As commented by Marius, an Activity might not be the optimal choice. If there is no user-input and the only thing the activity does is call a webservice and return the result, a method called from your Main Activity would be sufficient.
Firstly, calling finish() and starting the activity, NOT A GOOD IDEA.
Secondly, As far as i have understood your scenario, calling an AsyncTask inside a Timer after every 10 seconds is a better way to accomplish this. Call your web service in doInBackground() and then update your UI from onPostExecute(), this way you can avoid calling finish() and relaunching your activity every 10 seconds.
Finally you are creating a new Timer instance eveytime your Activity is called, so creating a huge number of Timer instances hanging your application.

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()

Making Periodic URL Requests from an Activity

I have an activity which makes periodic requests (once every 15 seconds to get a json data feed). The requests I am passing off to a AsyncTask so its not on the main UI thread. So far so good. But lets say that I request the feed and it takes 20 seconds to respond. I really don't want to kick off another thread until say 30 seconds are up. So ....
Is there a way to prevent the AsyncTask from running if there first one has not yet finished?
Also is there a way to timebox the AsyncTask to take no more than 30 seconds? reguardless of the Http timeout?
Thanks,
Steve
You can use a mutex variable to enforce this. Say, We call the variable update. This will be a boolean variable with initial value true.
Before running an Async task, Check the value of update, If it is true run the async task and set update to false.
After the end of the async task, in the onPostExecute() method, set update to true again.
So another async task will not be launched while one hasn't finished, thus ensuring exclusivity.

Android: Wait for the longer running process [Timer vs AsyncTask]

When my app is loading data in an AsyncTask, it shows a splash screen. Sometimes this data loading takes under a second and sometimes it takes much longer. However I want to ensure that the splash is up for at least 2 seconds if the data loading finished first, or otherwise wait until the data was done.
My first solution was to use a Countdown Timer, and two boolean values. When the processes would start, their booleans would be set to true. When the process was done, it would set it's boolean false. Then it would check if the other's boolean was false, and if it was dismiss the splash.
While this works, I feel like it is overly complicated and was hoping to find a more efficient or better solution.
I want to ensure that the splash is up for at least 2 seconds if the
data loading finished first, or otherwise wait until the data was
done.
You can asyncronously start a Thread(Splash) and AsyncTask for Loading data and call the new Activity on onPostExecute() of you AsyncTask.
Could you not use System.getCurrentTime(); To accomplish this?
At the start of the AsyncTask, call that method and store the result.
then, when the task finishes, call the method again and calculate the difference in the time values. If its not greater than 2000 milliseconds, have a while loop that continually requests the system time and compare the values until its equal to or greater than 2 milliseconds.

Android Running Timer

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.

Categories

Resources