Last active
August 29, 2015 14:27
-
-
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Exercise: AsyncTask for Background Processing\n", | |
"---\n", | |
"\n", | |
"## Objectives and Outcomes\n", | |
"\n", | |
"In this exercise you will learn about how to do background processing using the AsyncTask. You will also learn about the use of the Picasso image downloading library for downloading images asynchronously and then updating the UI. At the end of this exercise you will be able to:\n", | |
"\n", | |
"* Use the AsyncTask to offload work from the main UI thread to the background\n", | |
"* Make use of the Picasso image downloading library for asynchronous downloads\n", | |
"\n", | |
"## Introduction\n", | |
"\n", | |
"This project leverages the AsyncTask class available in Android to offload large computation to the background. The AsyncTask class provides an excellent and easy way to implement situations where you wish to do a major part of your work in the background, but it still needs the flexibility to update the UI at regular intervals. In addition, we will use the Picasso image downloading library to help us asynchronously download the avatar images of the friends and update the UI with their images.\n", | |
"\n", | |
"## Textual Instructions\n", | |
"---\n", | |
"\n", | |
"### Step 1: Examining the Project Files\n", | |
"\n", | |
"1. Download the [ChatClientAsync.zip](http://w02.hkvu.hk/edX/COMP107x/w5/ChatClientAsync.zip) file, unzip it and import the project into Android Studio.\n", | |
"2. Open the **Contacts.java** file.\n", | |
"3. Open the **MyArrayAdapter.java** file.\n", | |
"4. Follow the video instructions.\n", | |
"\n", | |
"---\n", | |
"\n", | |
"### Step 2: Updating the Contacts Activity\n", | |
"\n", | |
"1. Replace the // TODO comment in Contacts.java in the onCreate() method with the following code:\n", | |
" FriendsProcessor mytask = new FriendsProcessor();\n", | |
" mytask.execute(\"friendsjson.txt\");\n", | |
"2. Replace the // TODO comment in Contacts.java file in the AsyncTask class' doInBackground() method with the following code:\n", | |
" inputStream = mContext.getAssets().open(strings[0]);\n", | |
"\n", | |
"\n", | |
" in = new BufferedReader(new InputStreamReader(inputStream));\n", | |
"\n", | |
" String readLine;\n", | |
" StringBuffer buf = new StringBuffer();\n", | |
"\n", | |
" while ((readLine = in.readLine()) != null) {\n", | |
" buf.append(readLine);\n", | |
" }\n", | |
"\n", | |
" // Convert the read in information to a Json string\n", | |
" String infoString = buf.toString();\n", | |
"\n", | |
" // now process the string using the method that we implemented in the previous exercise\n", | |
" processFriendInfo(infoString);\n", | |
"\n", | |
" if (null != in) {\n", | |
" in.close();\n", | |
" }\n", | |
"\n", | |
"---\n", | |
"### Step 3: Updating MyArrayAdapter Class\n", | |
"\n", | |
"1. Replace the // TODO comment in **MyArrayAdapter.java** file with the following code:\n", | |
" ImageView imageView = (ImageView) friendInfoView.findViewById(R.id.avatar);\n", | |
" \n", | |
" Picasso.with(context).load(\"file:///android_asset/\"+friendInfoList.get(position).imageURL).into(imageView);\n", | |
"2. Run the app and see the result." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment