android thread manager: dynamically change task order - android

in my app, I want to implement an image viewer. The viewer needs to download a large amount of images from my own server using urls.
I want to do this in background (not block the UI).
But the tricky part is I need to dynamically change the order of the downloading task by the main activity. For example, there are 3 tasks right now, and the user click a button in the main activity, then the app needs to add a new task, but also the new coming task need to be execute first (with highest priority)?
How should I do this?
thanks a lot!

Lucklily you csn't do any mess like you wanted on system level. Use IntentService with task queue and manage your background jobs there

Related

Loading Animations while app gather's data

I'm working with an app that grabs a decent amount of data upon start each time since I need to immediately know things like location and weather fcst in the area.
In my mind I see a loading animation that smoothly moves around for 3 to 7 seconds while everything runs so there isn't just a blank screen or a static title image.
BUT:: I'm running into issues with getting animations or images to load before all of the data is searched for. And I only want the animation to run as long as data is loading. So I have the application switch to a new activity as soon as everything is loaded.
Is there a way to for the animation to run before things are done or do I need to somehow set everything up as an async task. And then how do I pass the async task to the next activity?
So I found an answer to my own question after reading some books. In short, the loading screen that i was looking for uses a splash screen with asynchronous tasks. Either AsyncTask directly or you can implement a service solution.
Extra: I noticed that if you run an async task which starts a GoogleAPIClient, onConnected executes on a pool thread or something, point being you can do UI work on the main thread again without wrapping back to AsyncTask.

android anyctask blocks activity

I am working on a project that lists items that users can click on any of them and see its detail in detailsActivity and if they like it so the can download it! i am using asynctask to list all the items from server in the doBackgournd method and display the result in onPostExecute method. The problem is when users start a download and go back to the list and click on another item the detail of the item is displayed only when the first download finishes!
I want the users be able to choose any item and go the detailsActivity while the download is running for other items they previously started.
please help
You should try and place your download to a Service.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
AsyncTask in (earlier versions after android 3.0 I think) android works sequentialy by default
if you want to change this behavior you can use executeOnExcuter instead of execute
for example
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

AsyncTask pause and resume

I have a activity that show one listview. In the activity I have a one AsyncTask (named here of AsListView) to get values from internet and fill some informations in each item of the listview. Work fine.
Now I created a button in ActionBar to show one image from streetview. To do this I have implemented another AsyncTask (named here of AsImage) to get image from google and show in a DialogFragment, but is necessary wait the execution of all AsListView Threads. It spends long time depending of the number of items in the list.
To execute AsImage rapidily, I cancel all AsListView tasks, but it's not good for me (user loss informations). The ideal soluction is set AsListView tasks to wait while AsImage execute. When AsImage finish I set AsListView tasks to continue execution. But I know that is not possible handle the control of execution of AsynkTasks.....
Some solution?
You can try to synchronize the AsyncTasks using a CountDownLatch.
Otherwise if you want you can use Threads instead of AsyncTasks and set their priorities, but there is a reason android made the AsyncTask class so I recommend you use it.

Prevent screen display until after fetching data

Is there any way I can go from one activity to another and delay the display of the screen on the target activity?
What I want to be able to do is to allow the target activity to fetch its required data but not to display anything until does.
I want the screen of the source activity to still be visible until I am ready with the data in the second activity.
Specifically, I am using an AsyncTask to fetch the data.
I know I could fetch the data in the source activity and then send it on to the target activity but this is not viable in our case.
Edit: More Info:
The whole reason I want this is because I am trying to change the structure of certain parts of the current code.
At present the way it works is the the first activity gets the data and then sends it to the second activity as a bundle.
This created problems when the second activity could be invoked from multiple places. It resulted in loads of duplicate code.
So, I decided to move the fetching of the data into the target activity thus cutting out any need for repeating code.
it also makes more sense for the activity to fetch its own data rather than relying on something else sending it.
You should first make a service that runs your async task. Then, start the service from your first activity with startService(new Intent(this, UpdaterServiceManager.class));
When the task ends in the service, start the second activity.
Click here for an excellent service tutorial.
Try to use service for this purpose.

Loading an Android UI avoiding locking

I'm building a news reader for Android, where the first Activity will show a list of the latest news, combined with a thumbnail preview image.
In order to get the thumbnail I have to run a method, which increase heavily the loading time; so, I was thinking to create a separate thread to run everytime.
More specifically I'd like to load the news titles first, and then load the pictures, one by one; while doing all this I don't want the UI to be locked (for instance, if an user touches a news I want the app to load it, even if thare are some thumbnails still loading).
My question is: should I use handlers (one thread for each news) or AsyncTask (one asyncTask object for each news) to achieve this?
Thank you for your replies.
Handler Vs AsyncTask
I would use an asynctask to download all the "news links" and then have that asynctask call an asynctask to download each thumbnail and update the UI onPostExecute. Then, if the user clicks a link before it is done, you could call cancel on the main Asynctask, which would check for isCancelled() between each thumbnail asynctask and would return if it was cancelled.
No doubt that AsyncTasks are more simplified and modularized than the thread-handler architechture, but internally they perform the action in same way.Coming to your problem, I would suggest that load the news first.Your news pojo/class can be like containing two feilds, title and imageUrl.Now display the news list and start another AsyncTask that fetches the images one by one and store them in a Data Str/list.your adapter should be "notifyDataSetChanged()" every time the image is fetched from server.
This way you are allowing user to see the news first, and the images are loaded without making UI get blocked.
i wit make asynch task for loading the data and then excute a task thats populate the list when the async task is don ore while it is running

Categories

Resources