Tip:
Highlight text to annotate it
X
This video explores the AsyncTask - a tool provided by Android to perform short term processing in the background.
This is part one in a 4 part series on AsyncTasks.
The objectives are stated here with a timeline for when they appear in the video.
It will be helpful for this video if you read the documentation on "AsyncTask" at the Android Developer website.
It will be helpful for this video if you read the documentation on "AsyncTask" at the Android Developer website.
OK, so it's time to take a look at doing some processing in the background.
Now, when we think about doing processing in the background, there are really two categories:
short-term process in the background, long-term process in the background.
Scenarios: you are on an Activity and you need to download a photograph, or do a short video,
or do a database query that may take more than just a moment.
You are going to want to put those things in their own thread of execution,
because if you don't, then you're blocking your user interface.
(Whenever you are in an Activity and you do a task that just takes more than a moment,
while that task is going on the user of your application is not able to interact with the user interface,
and it will bring up an "Activity not responding" message
and will kill off your Activity.)
So it's crucial that for anything that just takes more than a moment you spawn it off into its own thread of execution.
And instead of having to manage your own threads and create your own handlers and all that,
if you haven't done that before,
Android makes a nice little utility for you called the AsyncTask that's made to handle that.
And that's what we are going to be looking at today.
The other scenario you might be involved with is: maybe you're making a stock-tracking application (something like that)
and you want to, for a long period of time, say, "Hey, has the price changed? Hey, has the price changed?...
Let's check the price of that stock now."
And you want to have a very long running separate thread of execution out there
that's constantly performing some task at some interval.
That might be a time that you may want to use what's called a Service.
We won't be handling Services in these first sets of videos.
We will be stopping (in terms of threading) at the AsyncTask.
But I intend on following this set of videos up with an advanced Android programming set of videos
where we will dive into using Services.
All I've done to get ready for this demo is create a project called "AsyncTaskNoFrag."
I called it "NoFrag" because we are going to solve it twice:
once without Fragments, and once with Fragments.
From API 11 forward they recommend that you put your AsyncTask inside of Fragments.
Before API 11 you didn't have Fragments available
and so we will solve it the classic way when Fragments weren't available;
however, of course, we learned that you can still use your compatibility package if you choose to,
and then still use Fragments for an AsyncTask in earlier APIs.
In addition to that, I came into the MainActivity
and I deleted my onCreateOptionsMenu method because I'm not planning on adding a menu,
and I deleted my "menus" folder from my "res" folder for the same reason.
And I came into my main_activity.xml layout file and changed from RelativeLayout to LinearLayout
and set its orientation to vertical.
And that's all the pre-work that I've done on this project.
So let's dive into the user interface first, and come in here and place a Button on the screen.
And this Button is just going to be to start that background process in motion.
Now when you design your Activity, you might not need a Button for that.
(You might just instantly go out and do some database query
and the user may not be in control of starting the AsyncTask; you may be.)
But for the purposes of this demo we are going to