Android Develop and Design
Android Develop and Design
Programming
Develop and Design
Kevin Grant
Chris Haseman
Beginning Android
Programming
Develop and Design
www.peachpit.com
Peachpit Press
www.peachpit.com
To report errors, please send a note to [email protected]
Peachpit Press is a division of Pearson Education.
Copyright 2014 by Kevin Grant and Chris Haseman
Editor: Clifford Colby
Development editor: Robyn Thomas
Production editor: Danielle Foster
Copyeditor: Scout Festa
Technical editors: Matthew Brochstein and Vijay Penemetsa
Cover design: Aren Straiger
Interior design: Mimi Heft
Compositor: Danielle Foster
Indexer: Valerie Haynes Perry
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting
permission for reprints and excerpts, contact [email protected].
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in
the preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity with respect to
any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the
computer software and hardware products described in it.
Trademarks
Android is a trademark of Google Inc., registered in the United States and other countries. Many of the designations used by
manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this
book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All
other product names and services identified throughout this book are used in editorial fashion only and for the benefit of
such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended
to convey endorsement or other affiliation with this book.
ISBN-13: 978-0-321-95656-9
ISBN-10:
0-321-95656-7
987654321
Printed and bound in the United States of America
To my love, Erica, whos encouraged me to dream bigger than Ive ever imagined;
my mother, Jnette, who is my best friend and biggest fan;
and my grandmother, Helene, who always supported me in all of my endeavors.
Kevin Grant
Acknowledgments
As always, I could spend more pages thanking people than are in the work itself. Here are a
few who stand out:
Cliff C. for getting me on board (and always letting me know the weather). Robyn T. for
her diligence in keeping us all on time, and deleting all of my superfluous words. Scout F.
for her tolerance of my grammar. Matthew B. for helping out while taking care of his new
baby boy. Vijay P. for coming in under a tight deadline and working till the end. The mobile
team at Tumblr for their encouragement (cleverly disguised as snark). The Android team at
Google for building great new tools and making every release feel like a birthday. Most of all,
Peachpit for giving me the opportunity to write for you.
iv
Acknowledgments
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Welcome to Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Chapter 1
Chapter 2
vi
Contents
Chapter 4
Acquiring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
You There, Fetch Me That Data! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Watchdogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
What Not to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
When Am I on the Main Thread? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Getting Off the Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Getting Back to Main Land . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
There Must Be a Better Way! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
The AsyncTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
How to Make It Work for You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
A Few Important Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The IntentService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Declaring a Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Fetching Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Contents
vii
Chapter 6
Chapter 7
viii
Contents
Chapter 9
Contents
ix
Contents
Contents
xi
Introduction
If youve got a burning idea for an application that youre dying to share, or if you recognize
the power and possibilities of the Android platform, youve come to the right place. This is a
short book on an immense topic.
We dont mean to alarm anyone right off the bat here, but lets be honest: Android development is hard. Its architecture is dissimilar to that of many existing platforms (especially
other mobile SDKs), there are many traps for beginners to fall into, and you might find
yourself running to the Internet for answers. In exchange for its difficulty, however, Googles
Android offers unprecedented power, control, andyesresponsibility to those who are
brave enough to develop for it.
This is where our job comes in. Were here to make the process of learning to write amazing Android software as simple as possible.
Who are we to ask such things of you? Chris Haseman has been writing mobile software
in a professional capacity for ten years, and for five of those years, hes been developing
software for Android. Hes also written code that runs on millions of handsets throughout
the world. Also, he has a beard. We all know that people with ample facial hair appear to be
more authoritative on all subjects.
Kevin Grant has been developing for Android since its inception and has worked on a
breadth of user-facing products, developing beautiful and intuitive interfaces for millions of
users. While he doesnt have a beard, we all know that people with a perpetual five oclock
shadow know how to get things done.
From here on out, were going to take this conversation into the first person. We banter
enough amongst ourselvesits not necessary to confuse you in the process. So without further ado, in return for making this learning process as easy as possible, I ask for a few things:
JJ
You have a computer. My third-grade teacher taught me never to take anything for
granted; maybe you dont have a computer. If you dont already have a computer, youll
need onepreferably a fast one, because the Android emulator and Eclipse can use up a
fair amount of resources quickly.
personally develop on a Mac, you can use any of the three major platforms
(Mac, PC, or Linux).
JJ
xii
Introduction
Youre fluent in Java. Notice that I say fluent, not expert. Because youll be writing usable
applications (rather than production libraries, at least to start), I expect you to know the
differences between classes and interfaces. You should be able to handle threads and
concurrency without batting an eyelash. Further, the more you know about what happens under the hood (in terms of object creation and garbage collection), the faster and
better your mobile applications will be.
Yes, you can get through the book and even put together rudimentary applications without knowing much about the Java programming language. However, when you encounter
problemsin both performance and possibilitiesa weak foundation in the programming language may leave you without a solution.
JJ
You have boundless patience and endless curiosity. Your interest in and passion for
Android will help you through the difficult subjects covered in this book and let you glide
through the easy ones.
Throughout this book, I focus on how to write features, debug problems, and make interesting software. I hope that when youve finished the book, youll have a firm grasp of the
fundamentals of Android software development.
All right, thats quite enough idle talking. Lets get started.
Note: If youre more interested in the many whys behind Android, this
book is a good one to start with, but it wont answer every question you
may have.
Introduction
xiii
Welcome to Android
Eclipse and Android Studio are the two supported integrated development environments (IDEs) for Android development, and you need only
one to follow along with the examples in this book. There are, however,
a few other tools you should be aware of that will be very useful now and
in your future work with Android. While you may not use all these tools
until youre getting ready to ship an application, it will be helpful to know
about them when the need arises.
xiv
Android Studio
Android SDK
Welcome to Android
Android Virtual
Device Manager
Android Virtual Device
Manager is for those
developers who prefer to
develop on an emulator
rather than an actual
device. Its a little slow,
but you can run an
Android emulator for
any version of Android,
at any screen size. Its
perfect for testing screen
sizes, screen density, and
operating system versions across a plethora of
configurations.
Hierarchy Viewer
Monitor
Gradle
Welcome to Android
xv
Chapter 4
Acquiring Data
83
This is exactly what I did when initially faced with the same problem. While this code
will fetch and display the required bitmap, there is a very sinister issue lurking in the code
namely, the code itself is running on the main thread. Why is this a problem? Consider that
there can be only one main thread and that the main thread is the only one that can interact
with the screen in any capacity. This means that while the example code is waiting for the
network to come back with image data, nothing whatsoever can be rendered to the screen.
84
This image-fetching code will block any action from taking place anywhere on the device.
If you hold the main thread hostage, buttons will not be processed, phone calls cannot be
answered, and nothing can be drawn to the screen until you release it.
Watchdogs
Given that a simple programmer error (like the one in the example code) could effectively
cripple any Android device, Google has gone to great lengths to make sure no single application can control the main thread for any length of time. Starting in Android Honeycomb
(3.0), if you open any network connections on the main thread, your application will crash.
If youre hogging too much of the main threads time with long-running operations, such as
calculating pi or finding the next prime number, your application will produce this disastrous dialog box (Figure 4.1) on top of your application.
This dialog box is unaffectionately referred to by developers as an ANR (App Not
Responding) crash. Although operations will continue in the background, and the user can
press the Wait button to return to whatevers going on within your application, this is catastrophic for most users, and you should avoid it at all costs.
85
What Not to Do
What kind of things should you avoid on the main thread?
JJ
JJ
JJ
JJ
Any task that blocks a thread while you wait for something to complete
Excluding this list, there isnt much left, so as a general rule, if it doesnt involve setup or
modification of the user interface, dont do it on the main thread.
86
(HttpURLConnection) url.openConnection();
if(httpCon.getResponseCode() != 200){
throw new Exception(Failed to connect);
}
InputStream is = httpCon.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView iv = (ImageView)findViewById(R.id.remote_image);
iv.setImageBitmap(bt);
}catch(Exception e){
//handle failure here
}
}
}.start();
}
There, your enterprising hacker friend might say, Ive fixed your problem. The main
thread can continue to run unimpeded by the silly PNG downloading code. There is, however, another problem with this new code. If you run the method on your own emulator,
youll see that it throws an exception and cannot display the image onscreen.
Why, you might now ask, is this new failure happening? Well, remember that the main
thread is the only one allowed to make changes to the user interface. Calling setImage
Bitmap is very much in the realm of one of those changes and, thus, can be done only
whileon the main thread.
87
Remember, were already running in a thread, so accessing just this will refer to the
thread itself. I, on the other hand, need to invoke a method on the activity. Calling Image
Activity.this provides a reference to the outer Activity class in which weve spun up this
hacky code and will thus allow us to call runOnUiThread. Further, because I want to access
the recently created bitmap in a different thread, Ill need to make the bitmap declaration
final or the compiler will get cranky with us.
When you call runOnUiThread, Android will schedule this work to be done as soon as the
main thread is free from other tasks. Once back on the main thread, all the same dont be a
hog rules again apply.
88
The AsyncTask
At its core, the AsyncTask is an abstract class that you extend and that provides the basic
framework for a time-consuming asynchronous task.
The best way to describe the AsyncTask is to call it a working thread sandwich. That is to
say, it has three major methods for which you can provide implementation.
JJ
onPreExecute takes place on the main thread and is the first slice of bread. It sets up the
task, prepares a loading dialog, and warns the user that something is about to happen.
JJ
doInBackground is the meat of this little task sandwich (and is also required). This
onPostExecute will be called once your work is finished (again, on the main thread), and
the results produced by the background method will be passed to it. This is the other
slice of bread.
Thats the gist of the asynchronous task. There are more-complicated factors that Ill
touch on in just a minute, but this is one of the fundamental building blocks of the Android
platform (given that all hard work must be taken off the main thread).
Take a look at one in action, and then well go over the specifics of it:
private class ImageDownloader extends AsyncTask<String, Integer, Bitmap>{
Override
protected void onPreExecute(){
//Setup is done here
}
@Override
protected Bitmap doInBackground(String... params) {
try{
URL url = new URL(params[0]);
HttpURLConnection httpCon =
(HttpURLConnection) url.openConnection();
if(httpCon.getResponseCode() != 200)
throw new Exception(Failed to connect);
}
InputStream is = httpCon.getInputStream();
return BitmapFactory.decodeStream(is);
The AsyncTask
89
}catch(Exception e){
Log.e(Image,Failed to load image,e);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... params){
//Update a progress bar here, or ignore it, its up to you
}
@Override
protected void onPostExecute(Bitmap img){
ImageView iv = (ImageView) findViewById(R.id.remote_image);
if(iv!=null && img!=null){
iv.setImageBitmap(img);
}
}
@Override
protected void onCancelled(){
// Handle what you want to do if you cancel this task
}
}
That, dear readers, is an asynchronous task that will download an image at the end of
any URL and display it for your pleasure (provided you have an image view onscreen with
the ID remote_image). Here is how youd kick off such a task from the onCreate method of
your activity.
public void onCreate(Bundle extras){
super.onCreate(extras);
setContentView(R.layout.image_layout);
ImageDownloader imageDownloader = new ImageDownloader();
imageDownloader.execute(http://wanderingoak.net/bridge.png);
}
Once you call execute on the ImageDownloader, it will download the image, process it
into a bitmap, and display it to the screen. That is, assuming your image_layout.xml file
contains an ImageView with the ID remote_image.
90
JJ
JJ
The type of parameter that will be passed into the class. In this example AsyncTask code,
Im passing one string that will be the URL, but I could pass several of them. The parameters will always be referenced as an array no matter how many of them you pass in.
Notice that I reference the single URL string as params[0].
The object passed between the doInBackground method (off the main thread) and the
onProgressUpdate method (which will be called on the main thread). It doesnt matter in
the example, because Im not doing any progress updates in this demo, but itd probably
be an integer, which would be either the percentage of completion of the transaction or
the number of bytes transferred.
The object that will be returned by the doInBackground method to be handled by the
onPostExecute call. In this little example, its the bitmap we set out to download.
Heres the line in which all three objects are declared:
In this example, these are the classes that will be passed to your three major methods.
onPreExecute
called on the main thread, so dont touch the file system or network at all in this method.
doInBackground
This is your chance to make as many network connections, file system accesses, or other
lengthy operations as you like without holding up the phone. The class of object passed to
this method will be determined by the first generic object in your AsyncTasks class declaration. Although Im using only one parameter in the code sample, you can actually pass any
number of parameters (as long as they derive from the saved class), and youll have them
at your fingertips when doInBackground is called. Once your long-running task has been
completed, youll need to return the result at the end of your function. This final value will
be passed into another method called back on the main UI thread.
The AsyncTask
91
Theres another aspect of the AsyncTask that you should be aware of even though I havent
demonstrated it. From within doInBackground, you can send progress updates to the user
interface. doInBackground isnt on the main thread, so if youd like to update a progress bar
or change the state of something on the screen, youll have to get back on the main thread to
make the change.
Within the AsyncTask, you can do this during the doInBackground method by calling
publishProgress and passing in any number of objects deriving from the second class in
the AsyncTask declaration (in the case of this example, an integer). Android will then, on the
main thread, call your declared onProgressUpdate method and hand over any classes you
passed to publishProgress. Heres what the method looks like in the AsyncTask example:
protected void onProgressUpdate(Integer... params){
//Update a progress bar here, or ignore it, its up to you
}
As always, be careful when doing UI updates, because if the activity isnt currently
onscreen or has been destroyed, you could run into some trouble. The section A Few
Important Caveats discusses the bad things that can happen.
onPostExecute
The work has been finished, or, as in the example, the image has been downloaded. Its time
to update the screen with what Ive acquired. At the end of doInBackground, if successful, I
return a loaded bitmap to the AsyncTask. Now Android will switch to the main thread and
call onPostExecute, passing the class I returned at the end of doInBackground. Heres what
the code for that method looks like:
92
I take the bitmap downloaded from the website, retrieve the image view into which its going
to be loaded, and set it as that views bitmap to be rendered. Theres an error case I havent correctly handled here. Take a second to look back at the original code and see if you can spot it.
JJ
Make sure that the data youre moving with the AsyncTask pertains to only one activity,
because your task generally shouldnt span more than one. You can pass it between activities
if the screen has been rotated, but this can be tricky.
There are a few cases when its not a good idea to use an AsyncTask:
JJ
JJ
JJ
Any acquired data that may pertain to more than one activity shouldnt be acquired
through an AsyncTask. Both an image that might be shown on more than one screen and
a list of messages in a Twitter application, for example, would have relevance outside a
single activity.
Data to be posted to a web service is also a bad idea to put on an AsyncTask for the following reason: Users will want to fire off a post (posting a photo, blog, tweet, or other
data) and do something else, rather than waiting for a progress bar to clear. By using an
AsyncTask, youre forcing them to wait around for the posting activity to finish.
Last, be aware that there is some overhead for the system in setting up the AsyncTask.
This is fine if you use a few of them, but it may start to slow down your main thread if
youre firing off hundreds of them.
You might be curious as to exactly what you should use in these cases. Im glad you are,
because thats exactly what Id like to show you next.
The AsyncTask
93
The IntentService
The IntentService is an excellent way to move large amounts of data around without
relying on any specific activity or even application. The AsyncTask will always take over the
main thread at least twice (with its pre- and post-execute methods), and it must be owned by
an activity that is able to draw to the screen. The IntentService has no such restriction. To
demonstrate, Ill show you how to download the same image, this time from the Intent
Service rather than the AsyncTask.
Declaring a Service
Services are, essentially, classes that run in the background with no access to the screen.
In order for the system to find your service when required, youll need to declare it in your
manifest, like so:
<?xml version=1.0 encoding=utf-8?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
package=com.peachpit.Example
android:versionCode=1
android:versionName=1.0>
<application
android:name=MyApplication
android:icon=@drawable/icon
android:label=@string/app_name>
<!Rest of the application declarations go here -->
<service android:name=.ImageIntentService/>
</application>
</manifest>
At a minimum, youll need to have this simple declaration. It will then allow you to
(asIshowed you earlier with activities) explicitly launch your service. Heres the code
todoexactly that:
Intent i = new Intent(this, ImageIntentService.class);
i.putExtra(url, getIntent().getExtras().getString(url));
startService(i);
At this point, the system will construct a new instance of your service, call its onCreate
method, and then start firing data at the IntentServices handleIntent method. The intent
service is specifically constructed to handle large amounts of work and processing off the
main thread. The services onCreate method will be called on the main thread, but subsequent calls to handleIntent are guaranteed by Android to be on a background thread (and
this is where you should put your long-running code in any case).
94
Right, enough gabbing. Let me introduce you to the ImageIntentService. The first thing
youll need to pay attention to is the constructor:
public class ImageIntentService extends IntentService{
public ImageIntentService() {
super(ImageIntentService);
}
Notice that the constructor you must declare has no string as a parameter. The parents
constructor that you must call, however, must be passed a string. Your IDE will let you know
that you must declare a constructor with a string, when in reality, you must declare it without one. This simple mistake can cause you several hours of intense face-to-desk debugging.
Once your service exists, and before anything else runs, the system will call your
onCreate method. onCreate is an excellent time to run any housekeeping chores youll need
for the rest of the services tasks (more on this when I show you the image downloader).
At last, the service can get down to doing some heavy lifting. Once it has been constructed and has had its onCreate method called, it will then receive a call to handleIntent
for each time any other activity has called startService.
Fetching Images
The main difference between fetching images and fetching smaller, manageable data is that
larger data sets (such as images or larger data retrievals) should not be bundled into a final
broadcast intent (another major difference to the AsyncTask). Also, keep in mind that the
service has no direct access to any activity, so it cannot ever access the screen on its own.
Instead of modifying the screen, the IntentService will send a broadcast intent alerting all
listeners that the image download is complete. Further, since the service cannot pass the
actual image data along with that intent, youll need to save the image to the SD card and
include the path to that file in the final completion broadcast.
The setup
Before you can use the external storage to cache the data, youll need to create a cache folder
for your application. A good place to check is when the IntentServices onCreate method
iscalled:
public void onCreate(){
super.onCreate();
String tmpLocation = Environment.getExternalStorageDirectory().getPath() +
p CACHE_FOLDER;
cacheDir = new File(tmpLocation);
if(!cacheDir.exists()){
cacheDir.mkdirs();
}
}
The IntentService
95
Using Androids environment, you can determine the correct prefix for the external file
system. Once you know the path to the eventual cache folder, you can then make sure the
directory is in place. Yes, I know I told you to avoid file-system contact while on the main
thread (and onCreate is called on the main thread), but checking and creating a directory is a
small enough task that it should be all right. Ill leave this as an open question for you as you
read through the rest of this chapter: Where might be a better place to put this code?
The fetch
Now that youve got a place to save images as you download them, its time to implement the
image fetcher. Heres the onHandleIntent method:
protected void onHandleIntent(Intent intent) {
String remoteUrl = intent.getExtras().getString("url");
String location;
String filename = remoteUrl.substring(
remoteUrl.lastIndexOf(File.separator) + 1);
File tmp = new File(
cacheDir.getPath() + File.separator + filename);
if (tmp.exists()) {
location = tmp.getAbsolutePath();
notifyFinished(location, remoteUrl);
stopSelf();
return;
}
try {
96
At this point, youve created the cache file, retrieved it from the web, and written it to the
aforementioned cache file. Its time to notify anyone who might be listening that the image
is available. Heres the contents of the notifyFinished method that will tell the system both
that the image is finished and where to get it.
public static final String TRANSACTION_DONE =
com.peachpit.TRANSACTION_DONE;
private void notifyFinished(String location, String remoteUrl){
Intent i = new Intent(TRANSACTION_DONE);
i.putExtra(location, location);
i.putExtra(url, remoteUrl);
ImageIntentService.this.sendBroadcast(i);
}
The IntentService
97
Anyone listening for the broadcast intent com.peachpit.TRANSACTION_DONE will be notified that an image download has finished. They will be able to pull both the URL (so they can
tell if it was an image it actually requested) and the location of the cached file.
Rendering the download
In order to interact with the downloading service, there are two steps youll need to take.
Youll need to start the service (with the URL you want it to fetch). Before it starts, however,
youll need to register a listener for the result broadcast. You can see these two steps in the
following code:
public void onCreate(Bundle extras){
super.onCreate(extras);
setContentView(R.layout.image_layout);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ImageIntentService.TRANSACTION_DONE);
registerReceiver(imageReceiver, intentFilter);
Intent i = new Intent(this, ImageIntentService.class);
i.putExtra(url, getIntent().getExtras().getString(url));
startService(i);
pd = ProgressDialog.show(this,
Fetching Image,
Go intent service go!);
}
This code registered a receiver (so you can take action once the download is finished),
started the service, and, finally, showed a loading dialog box to the user.
Now take a look at what the imageReceiver class looks like:
private BroadcastReceiver imageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String location = intent.getExtras().getString(location);
if(TextUtils.isEmpty(location){
String failedString = Failed to download image;
Toast.makeText(context, failedString , Toast.LENGTH_LONG).show();
}
File imageFile = new File(location);
if(!imageFile.exists()){
pd.dismiss();
98
This is a custom extension of the BroadcastReceiver class. This is what youll need to
declare inside your activity to correctly process events from the IntentService. Right now,
there are two problems with this code. See if you can recognize them.
First, youll need to extract the file location from the intent. You do this by looking for the
location extra. Once youve verified that this is indeed a valid file, youll pass it over to the
BitmapFactory, which will create the image for you. This bitmap can then be passed off to
the ImageView for rendering.
Now, to the things done wrong (stop reading if you havent found them yetno cheating!). First, the code is not checking to see if the intent service is broadcasting a completion
intent for exactly the image originally asked for (keep in mind that one service can service
requests from any number of activities).
Second, the bitmap is loading from the SD card on the main thread! Exactly one of the
things Ive been warning you NOT to do.
The IntentService
99
The Loader
Loader is a new class that comes both in Honeycomb and in the Android Compatibility
library. Sadly, there is not enough space in this chapter to cover it in detail, but I will say
that its an excellent tool to explore if you must do heavy lifting off the main thread
repeatedly. It, like AsyncTask, is usually bound to an activity, but it is much better suited to
handle situations where a single task must be performed many times. The CursorLoader
subclass is great for loading cursors from your applications ContentProvider, and for
tasks like downloading individual list items for a ListView, there is an AsyncTaskLoader.
Check the documentation for how best to use this new and powerful class.
Wrapping Up
That about covers how to load data. Remember, loading from the SD card, network transactions, and longer processing tasks MUST be performed off the main thread, or your application, and users, will suffer. You can, as Ive shown you in this chapter, use a simple thread, an
AsyncTask, or an IntentService to retrieve and process your data. But remember, too, that
any action modifying any view or object onscreen must be carried out on the main thread
(orAndroid will throw angry exceptions at you).
Further, keep in mind that these three methods are only a few of many possible background data fetching patterns. Loaders, Workers, and ThreadPools are all other alternatives
that might suit your application better than the examples Ive given.
Follow the simple rules Ive outlined here, and your app will be fast, it will be responsive
to your users, it shouldnt crash (ha!), and it will avoid the dreaded App Not Responding
notification of doom. Correct use and avoidance of the main thread is critical to producing
asuccessful application.
If youre interested in building lists out of complex data from remote sources, the next
chapter should give you exactly what youre looking for. Ill be showing you how to render
alist of Twitter messages to a menu onscreen.
Ill leave you with a final challenge: Enable Androids strict mode and move the little file
accesses Ive left in this chapters sample code off the main thread. It should be a good way to
familiarize yourself with the process before you undertake it on your own.
100
Index
Numbers
0 arguments
PendingIntent, 130
requestCode, 130
using with communication, 130
Symbol
: (colon), using with binder services, 134
A
AbsoluteLayout, 6870
action bar
action views, 204
adding elements to, 204208
AppCompat library, 200203
documentation, 208
explained, 200
menu items, 204205
showing, 204
tabs, 204, 207208
view pager, 212213
action views, using, 207
ActionBar.TabListener, implementing, 207
ActionBarToggleDrawer, setting as listener, 221
ActionBarToggleDrawer arguments
Activity, 220
CloseDrawerContentDescription, 220
DrawerImageResource, 220
DrawerLayout, 220
OpenDrawerContentDescription, 220
activities
basics, 26
creating screen layout, 2829
data retention methods, 3536
vs. fragments, 192
handling collisions, 4243
246
Index
implementing, 2631
launching, 2931
lifecycles, 3233
methods, 32
NewActivity class, 27
onCreate method, 3233
onDestroy method, 32, 35
onPause method, 32, 34
onResume method, 32
onStart method, 32
onStop method, 32, 34
public void onCreate(), 3334
public void onResume(), 34
public void onStart(), 34
pushing button, 2930
registering for events, 39
running, 34
saving primitives, 36
trying out, 31
Activity class
controlling single screens, 25
extending, 2526
getting back to main thread, 88
activity declaration, adding, 26
Adapter class
customizing, 114116
explained, 104
getCount(), 114
getItem(), 114
getItemId(), 114
getView(), 114, 117
interaction with ListView, 119121
ADB (Android Debug Bridge), restarting, 21
ADT Bundle. See Eclipse (ADT Bundle)
AIDL (Android Interface Definition Language),
133134
Android Debug Bridge (ADB), restarting, 21
Android projects
creating, 1416
R.java file, 63
running, 1820
types, 14
view pager, 212
Android SDK
accessing, 121
updating, 78
Android Studio
AppCompat library, 200203
creating key in, 231
creating projects, 14
exporting release build, 229
features, 4
installing, 6
keystore file, 230
maps, 182183
running projects in, 1920
updating Android SDK, 7
virtual device emulator, 9
Android versions
downloading, 8
handling older code, 151
SharedPreferences, 151
AndroidManifest.xml file, 238
android:name, 45
ANR (App Not Responding) crash, 8586
API key, using with maps, 185187
API levels, watching, 153
APK, producing final version of, 228231
AppCompat library, setting up, 200203
AppCompat project
adding as library project, 202
enabling, 202203
importing, 201
Application class
customizing, 46
default declaration, 45
applications
accessing, 4647
checking, 99
customizing, 4546
updating frequently, 232233
ArrayAdapter
creating, 108
populating, 108
AsyncTask abstract class
best practices, 93
doInBackground method, 89, 91
example, 8990
generic type arguments, 91
ImageDownloader, 90
onPostExecute method, 89, 9293
onPreExecute method, 89, 91
progress updates, 92
starting, 93
audio. See also sounds
calling play, 172173
onCompletionListener, 173174
playing in music service, 169174
setDataSource, 169174
auto image uploading, 126
AVD (Android Virtual Device) Manager, 912
B
background color
changing for list view, 117
gray, 7980
backing up keystore file, 231
binary format, packed, 63
binder interfaces, using with services, 125
binder services. See also communication
: (colon), 134
AIDL (Android Interface Definition
Language), 133134
binder and AIDL stub, 135136
binding, 136137
communicating with, 136137
creating services, 134135
IMusicService, 135
marshaling process, 134
requirements, 133
Index
247
BroadcastReceiver
class, 99
instance, 3943
build files, adding signing keys to, 244245
build types, using with Gradle files, 239241
build variants, using with Gradle, 243244
BuildConfig, adding values to, 241
builds, submitting, 232
buildscript, using with Gradle files, 237
Build.VERSION_CODE.GINGERBREAD, 152
Build.VERSION.SDK_INT, 152153
buttons
layout folders example, 142143
pushing, 2930
sizes in LinearLayout, 7475
C
cache folder, using with IntentService,
9596
CameraUpdates, using with maps, 187188
checking applications, 99
click events, reacting to, 108109
click listener, registering, 57
code, handling in older versions, 151
colon (:), using with binder services, 134
command line
directories for installation, 56
using in Eclipse, 17
communication. See also binder services;
intent-based communication; services
binder interfaces, 125
intent broadcasts, 125
intent-based, 126133
console statistics, seeing, 232233
ContentProvider, using in communication,
128129
crash reports, watching and fixing, 232
cursor loader, using for music playback, 166
custom views. See also views
class declaration, 59
extending, 59
customizing applications, 4546
248
Index
D
data. See also loading data
creating for main menu, 104105
moving, 4345
data retention methods
onRetainNonConfigurationInstance,
3536
onSaveInstanceState, 35
debugging. See also troubleshooting
layout issues, 149
preventing, 226
DemoListFragment, 196
development environments
Android Studio, 4
Eclipse (ADT Bundle), 4
devices. See also working devices
unknown sources, 13
USB debugging, 13
working with, 1213
dialogs, loading, 92
dip or dp (device-independent pixels), 53, 67
drawable folders, contents, 6263, 6566
DummyFragment, using with getPageTitle, 217
E
Eclipse (ADT Bundle)
AppCompat library, 201
creating key in, 231
creating projects, 14
creating projects from command line, 17
exporting release build, 229
features, 4
installing, 5
keystore file, 230
maps, 182183
running projects in, 1819
updating Android SDK, 7
virtual device emulator, 9
emulator. See virtual device emulator
exceptions, handling, 113
exporting
release build in Android Studio, 229
release build in Eclipse, 229
signed build, 228231
F
file storage, 9596
files
AndroidManifest.xml, 24
manifest, 24
saving to SD cards, 96
FragmentActivity class
finding, 196
using with maps, 184
FragmentManager, using, 198
FragmentPagerAdapter
explained, 212
getCount method, 215
getItem method, 215216
Locale.getDefault() function, 217
overriding getPageTitle, 216217
fragments
vs. activities, 192
backward compatibility, 198200
checking for, 197
compatibility library, 199200
ContentFragment class, 194
creating, 193194
DemoListFragment, 196
explained, 192
Gradle file, 198
lifecycles, 192193
onCreate, 192
onCreateView, 192
onDestroy, 193
onDestroyView, 193
onDetach, 193
onPause, 192
onResume, 192
onStart, 192
onStop, 192
showing, 194198
single text view, 195
startup lifecycle, 192
swapping for navigation drawer, 222
FragmentStatePagerAdapter, 212
G
getCount method
Index
249
H
handling exceptions, 113
height and width, determining for views, 51, 53
I
@id/., referencing for layouts, 77
250
Index
J
JAR files, compiling, 239
Java
in Java, 5152
MATCH_PARENT definition, 53
text view, 52
views in, 5152
WRAP_CONTENT definition, 53
Java vs. XML layouts, 55
K
key
creating in Android Studio, 231
creating in Eclipse, 231
keystore file
backing up, 231
creating in Android Studio, 230
creating in Eclipse, 230
using with signing key, 245
L
landscape folder, using, 144
landscape layout, 72
layout folders
adding suffixes to, 148
contents, 62, 6465
creating new layouts, 148149
<include> tag, 144147
landscape folder, 144
<merge> tag, 147
MVC (Model-View-Controller), 65
screen with buttons, 142143
using, 144
layout issues, debugging, 149
layout management. See also picture viewer
AbsoluteLayout, 6870
landscape mode, 72, 75
LinearLayout, 7075, 107
for ListActivity, 106
RelativeLayout, 7680
ViewGroup class, 6667
LinearLayout
button size, 74
defining views in, 7075
match_parent definition, 73
pixels, 74
specifying dimension, 107
using, 7375
width setting, 73
Linux
installing Android Studio, 6
installing Eclipse (ADT Bundle), 5
list items, types of, 120121
list view
building, 116119
changing background color, 117
custom adapter, 114116
exceptions, 113
ListActivity, 111112
main layout view, 110111
Reddit data, 112114
RedditAsyncTask, 112114
subreddits, 112114
TextViews, 117118
ListActivity. See also menu list item
behavior, 109
creating, 105, 111112
declaring layout for, 106
ListView class
explained, 104
interaction with Adapter, 119121
Loader class, 100, 168169
loading data. See also data
AsyncTask abstract class, 8993
IntentService, 94100
main thread, 8488
locations. See also maps
getLastKnownLocation, 180
getting for devices, 178
onLocationChanged method, 180
permissions, 178
receiving updates, 179180
requestLocationUpdates method, 179180
service suppliers, 178179
logging, disabling prior to shipping, 230
Index
251
M
main menu
ArrayAdapter, 108
creating data, 104105
example, 109
ListActivity, 105106, 109
reacting to click events, 108109
main thread
ANR (App Not Responding) crash, 8586
AsyncTask abstract task, 8993
best practices, 86
considering for services, 125
getting back to, 88
getting off, 8788
IntentService, 9499
Loader class, 100
managing, 8485
verifying, 86
manifest files
AndroidManifest.xml, 24
android:name, 45
for maps, 183
manifest registration, 3738
map view
CameraUpdates, 187188
MarkerOptions, 187188
running, 187188
MapFragment component
adding to manifest, 183
creating, 184185
described, 181
getting, 181183
modifying, 184
maps. See also locations
adding to manifest, 183
adjusting activity, 184
API key, 185
FragmentActivity, 184
SDK manager options, 181
MarkerOptions, using with maps, 187188
marshaling process, explained, 134
252
Index
match_parent definition, 67
media. See also movies
loading data, 160161
OnDestroy method, 161
onErrorListener, 161
playing, 160161
media players, cleanup, 174
MediaPlayer states
Idle, 162
Initialized, 162
Playing, 162
Prepared, 162
MediaScanner, using, 159
menu. See main menu
menu items
adding to action bar, 205206
reacting to clicks, 206208
menu list item, creating, 107. See also
ListActivity
<merge> tag, using with layout folders, 147
messages, sending toasts, 41
movie playback process, 156
movies. See also media
adding VideoView, 156
getting media to play, 157159
passing URIs to video view, 159
setting up VideoView, 157
moving data, 4345
music
binding to music service, 165
cursor loader, 166
finding recent tracks, 165167
Idle state, 162
Initialized state, 162
Loader class, 168169
longer-running, 164
MediaPlayer and state, 162
playing sound effects, 163
playing sounds, 162163
Playing state, 162
Prepared state, 162
music playback
listening for intents, 167169
process, 164
music service, playing audio in, 169174
music software
audio focus, 174
headphone controls, 174
interruptions, 174175
missing SD cards, 175
phone calls, 174
MVC (Model-View-Controller), 65
N
navigation, view pager, 212
navigation drawer
ActionBarDrawerToggle, 220
ActionBarToggleDrawer, 220
demo, 218
explained, 217
onCreate, 218221
onItemClickListener, 219
setContentView, 219
setDisplayHomeAsUpEnabled, 219220
standard icon, 218
swapping fragments, 222
visible shadow, 219
XML, 221
NewActivity class, creating, 27
Next button, 7879
notification, using in communication, 130131
notifyFinished method, 9798
O
onBlind, using with services, 124
onClickListener, setting, 5658
onCompletionListener, calling for audio,
173174
onCreate method
P
packages, naming, 226227
packaging and signing, 228231
packed binary format, 63
padding declaration, 78
page change listener, creating, 214
PendingIntent, flags associated with, 130
Index
253
R
Reddit data, getting, 112114
RelativeLayout
gray background, 7980
nesting in, 80
Next button, 7879
padding declaration, 78
Prev button, 78
referencing @id/., 77
release build, exporting, 229
repositories, using with Gradle, 237
requestLocationUpdates method, 179180
res folder
contents, 6263
drawable folders, 6263, 6566
layout folders, 62, 6465, 142147
254
Index
naming conventions, 63
values folder, 62, 64
resource management, 6263
resources, finding, 54
R.java file, 63
S
saving
files to SD cards, 96
primitives, 36
screen layout, creating, 2829
screen sizes, handling, 6566
screen with buttons, 142143
screens, controlling, 25
SD cards, saving files to, 96
SDK Manager, opening, 7
SDK methods, version checking, 152
SDK value, setting minimum, 228
SDK version number, 150
services. See also communication
creating, 134135
declaring, 9495
explained, 124
keeping running, 125
lifecycles, 124
main thread, 125
onBlind, 124
onCreate, 124
onDestroy method, 125
onStartCommand, 124
startForeground method, 125
setContentView, using with navigation
drawer, 219
setDataSource, using with audio, 169174
setDisplayHomeAsUpEnabled, using with
navigation drawer, 219220
SharedPreferences, commit method, 151
signed build, exporting, 228231
signing key, adding to build files, 244245
sound effects, playing, 163
sounds, playing, 162163. See also audio
sp (scaled pixel), 53
startForeground method, using with
services, 125
storing files, 9596
StrictMode.enableDefaults(), 99
T
tablets
building layouts for, 198
rendering on, 198
text editor, using, 28
text view, customizing, 59
thread. See main thread
toast, explained, 41
tracks, finding for music, 165167
troubleshooting emulator, 21. See also
debugging
U
UI (user interface)
altering at runtime, 5355
finding resources, 54
identifying views, 5354
keeping views, 5455
XML vs. Java layouts, 55
unknown sources, allowing, 13
updating
Android SDK, 78
applications frequently, 232233
URIs, passing to video view, 159
USB debugging, enabling, 13
<uses> tag, using with working devices, 150
V
values folder
arrays, 64
colors, 64
contents, 62
dimensions, 64
strings, 64
styles, 64
Index
255
views (continued )
wrap_content definition, 53
WRAP_CONTENT definition, 53
in XML, 5051
virtual device emulator
Snapshot option, 12
troubleshooting, 21
Use Host GPU option, 12
using, 912
visibility, changing for views, 5558
W
width and height, determining for views, 51, 53
Windows
installing Android Studio, 6
installing Eclipse (ADT Bundle), 5
working devices. See also devices
limiting access to, 149151
SDK version number, 150
<uses> tag, 150
256
Index
X
XML
AbsoluteLayout, 6870
custom views, 6162
editing, 28
vs. Java layouts, 55
match_parent definition, 53
navigation drawer, 221
showing fragments, 194197
view pager, 215
views in, 5051
wrap_content definition, 53
XML files
packed binary format, 63
referencing resources, 63
XML terms
dip or dp (device-independent pixels), 67
match_parent definition, 67
px (pixels), 67
Sign up today
peachpit.com /creativeedge