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

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.

Related

Measuring app load times in 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.

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.

Two different AsyncTasks execute at the same time

I have two total different implementations of AsyncTask, lets say AT1 and AT2.
AT1 is executed first, then AT2 is executed. AT1 waits in doInBackground until AT2 has done its task by polling this data every 500 ms. But this never happens.
So what I basically want is this:
But what seems to happen is this:
Except AT1 is never done, and AT2 is never started.
Is there a way I can force these two AsyncTasks to be executed on two seperate threads, or is there another solution for this?
It is not possible to first start AT2 and after that execute AT1.
EDIT
For clarification: AT1 is executed when a user opens a particular screen, and needs to download data for that screen, based on a location. AT2 is executed on Location change, and when that happens, some calculations are made that cannot be done on the UI thread.
When AT2 has never been executed, AT1 has no location to download data for, so it needs to wait for AT2 to finish. When AT2 has been executed, the location data is already there, and AT1 doesn't need to wait.
Also, this problem occurs in ICS, not in Android 2.3, like this answer suggests.
When I posted this question, this question appeared in the Related section.
It advices to use executeOnExecutor, I've implemented this as follows:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new SetLocationAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
new SetLocationAsyncTask().execute(location);
}
What's the point of having two async tasks, if one waits for the other? And if you want to do it, why not have the first one start the second one and put the "done" stuff in the second task?
Sounds like you may need to rethink your logic - if you're kicking off a thread which then needs to wait for another thread to do some work, why not just have the first thread do that work?
If you really need 2 AsyncTasks, have the first one gather whatever data is needed and don't kick off the second until after the first is finished - have a read about onPostExecute in the docs.

Asynctask API call issue

I'm currently using an AsyncTask to make an API call and populate a list with data.
I have a Sub Menu whose items can call the AsyncTask to populate the data, problem is that if i click quickly i end up with merged results obviously cause the AsyncTask is running at the same time as each other.
What is the best way to handle a situation like this? Sorry if this is a amateur question.
I would use a ProgressDialog to show that content is being updated, and when the update is complete, dismiss() the dialog. While this is happening, you should make sure that you are not accepting touch input on your ListView. (this may happen by default when the ProgressDialog is in front, I am not remembering currently...)
Take a look at this link for an example.
You could configure only a single Asynch task to run at a time.
A boolean variable which is set to true as soon as the asynch task starts and set to false as soon as it finishes.
Next call could wait for this to be set to false. You could also rate limit what is the min time after which only making the API call makes sense.

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