Download Hello, Android PDF

TitleHello, Android
LanguageEnglish
File Size4.4 MB
Total Pages247
Table of Contents
                            Contents
Acknowledgments
Changes (Cupcake Updates)
	P1.5---July 21
	P1.4---July 6
	P1.3---June 22
	P1.2---June 9
	P1.1---May 26
	TODO in future releases
Preface
	What Makes Android Special?
	Who Should Read This Book?
	What's in This Book?
	FLAGCOLOR What's New for Cupcake?
	Online Resources
	Fast-Forward >>
Introducing Android
	Quick Start
		Installing the Tools
		Creating Your First Program
		Running on the Emulator
		Running on a Real Phone
		Fast-Forward >>
	Key Concepts
		The Big Picture
		It's Alive!
		Building Blocks
		Using Resources
		Safe and Secure
		Fast-Forward >>
Android Basics
	Designing the User Interface
		Introducing the Sudoku Example
		Designing by Declaration
		Creating the Opening Screen
		Using Alternate Resources
		Implementing an About Box
		Applying a Theme
		Adding a Menu
		Adding Settings
		Starting a New Game
		Debugging with Log Messages
		Debugging with the Debugger
		Exiting the Game
		Fast-Forward >>
	Exploring 2D Graphics
		Learning the Basics
		Adding Graphics to Sudoku
		Handling Input
		The Rest of the Story
		Making More Improvements
		Fast-Forward >>
	Multimedia
		Playing Audio
		Playing Video
		Adding Sounds to Sudoku
		Fast-Forward >>
	Storing Local Data
		Adding Options to Sudoku
		Continuing an Old Game
		Remembering the Current Position
		Accessing the Internal File System
		Accessing SD Cards
		Fast-Forward >>
Beyond the Basics
	The Connected World
		Browsing by Intent
		Web with a View
		From JavaScript to Java and Back
		Using Web Services
		Fast-Forward >>
	Locating and Sensing
		Location, Location, Location
		Set Sensors to Maximum
		Bird's-Eye View
		Fast-Forward >>
	Putting SQL to Work
		Introducing SQLite
		SQL 101
		Hello, Database
		Data Binding
		Using a ContentProvider
		Implementing a ContentProvider
		Fast-Forward >>
	3D Graphics in OpenGL
		Understanding 3D Graphics
		Introducing OpenGL
		Building an OpenGL Program
		Managing Threads
		Building a Model
		Lights, Camera, ...
		Action!
		Applying Texture
		Peekaboo
		Fast-Forward >>
Appendixes
	Java vs. the Android Language and APIs
		Language Subset
		Standard Library Subset
		Third-Party Libraries
	Hello, Widget
		Creating Your First Widget
		Calling All Widgets!
		Stretch to Fit
		The Rest of the Story
		Running the Widget
		Keeping Up to Date
		Go Wild
	Publishing to the Android Market
	Bibliography
Index
	A
	B
	C
	D
	E
	F
	G
	H
	I
	J
	K
	L
	M
	N
	O
	P
	Q
	R
	S
	T
	U
	V
	W
                        
Document Text Contents
Page 1

Prepared exclusively for Trieu Nguyen

Page 2

What readers are saying about Hello, Android

This is a most excellent book: very well written, easy to read, and fun.

In addition, any of Android’s quirks are explained along with just the

right amount of detail to ensure quality programming principles are

followed.

Anthony Stevens

Founder and CTO, PocketJourney and Top 20 Winner of

Google Android Competition

Ed Burnette covers an impressive amount of ground in a nicely com-

pact book while retaining the popular Pragmatic style. For the mate-

rial on 2D and 3D graphics alone, this is worthy of a spot in any

Android developer’s library.

Mark Murphy

Founder, CommonsWare

I remember when I first started to work with Android; it was like a

huge maze. With this book, the introduction would have been much

less painful. I am convinced that by reading this book new Android

programmers will have an easier start.

Gabor Paller

Senior Software Architect, OnRelay, Ltd.

Prepared exclusively for Trieu Nguyen

Download at Boykma.Com

Page 123

REMEMBERING THE CURRENT POSITION 123

Download Sudokuv4/src/org/example/sudoku/Sudoku.java

public void onClick(View v) {

switch (v.getId()) {

case R.id.continue_button:

startGame(Game.DIFFICULTY_CONTINUE);

break;

// ...

}

}

We added a case in Sudoku.onClick( ) to call startGame( ) when the Con-

tinue button is pressed, passing it DIFFICULTY_CONTINUE. startGame( )

passes the difficulty to the Game activity, and Game.onCreate( ) calls

Intent.getIntExtra( ) to read the difficulty and passes that to getPuzzle( )

(you can see the code for that in Section 4.2, Starting the Game, on

page 78).

There’s one more thing to do: restore from our saved game when our

activity goes away and comes back on its own (such as if another activ-

ity is started and then the user comes back to the Game activity). This

modification to the Game.onCreate( ) method will take care of that:

Download Sudokuv4/src/org/example/sudoku/Game.java

@Override

protected void onCreate(Bundle savedInstanceState) {

// ...

// If the activity is restarted, do a continue next time

getIntent().putExtra(KEY_DIFFICULTY, DIFFICULTY_CONTINUE);

}

That pretty much covers it for preferences. Next let’s look at saving

instance state.

6.3 Remembering the Current Position

If you change the screen orientation while Sudoku is running, you’ll

notice that it forgets where its cursor is. That’s because we use a cus-

tom PuzzleView view. Normal Android views save their view state auto-

matically, but since we made our own, we don’t get that for free.

Unlike persistent state, instance state is not permanent. It lives in a

Bundle class on Android’s application stack. Instance state is intended

to be used for small bits of information such as cursor positions.

Report erratum

this copy is (P1.5 printing, July 21, 2009)
Prepared exclusively for Trieu Nguyen

Download at Boykma.Com

http://media.pragprog.com/titles/eband/code/Sudokuv4/src/org/example/sudoku/Sudoku.java
http://media.pragprog.com/titles/eband/code/Sudokuv4/src/org/example/sudoku/Game.java
http://books.pragprog.com/titles/eband/errata/add?pdf_page=123

Page 124

REMEMBERING THE CURRENT POSITION 124

Here’s what we have to do to implement it:

Download Sudokuv4/src/org/example/sudoku/PuzzleView.java

Line 1 import android.os.Bundle;
- import android.os.Parcelable;
-

- public class PuzzleView extends View {
5 private static final String SELX = "selX";
- private static final String SELY = "selY";
- private static final String VIEW_STATE = "viewState";
- private static final int ID = 42;
-

10 public PuzzleView(Context context) {
- // ...
- setId(ID);
- }
-

15 @Override
- protected Parcelable onSaveInstanceState() {
- Parcelable p = super.onSaveInstanceState();
- Log.d(TAG, "onSaveInstanceState");
- Bundle bundle = new Bundle();

20 bundle.putInt(SELX, selX);
- bundle.putInt(SELY, selY);
- bundle.putParcelable(VIEW_STATE, p);
- return bundle;
- }

25 @Override
- protected void onRestoreInstanceState(Parcelable state) {
- Log.d(TAG, "onRestoreInstanceState");
- Bundle bundle = (Bundle) state;
- select(bundle.getInt(SELX), bundle.getInt(SELY));

30 super.onRestoreInstanceState(bundle.getParcelable(VIEW_STATE));
- return;
- }
- // ...
- }

On line 5, we define some constants for keys to save and restore the

cursor position. We need to save both our own x and y positions, plus

any state needed by the underlying View class.

As part of Activity.onSaveInstanceState( ) processing, Android will walk

down the view hierarchy and call View.onSaveInstanceState( ) on every

view it finds that has an ID. The same thing happens for onRestoreIn-

stanceState( ). Normally, this ID would come from XML, but since Puzzle-

View was created in code, we need to set it ourselves. We make up an

arbitrary number on line 8 (any value will do as long as it’s positive)

and then use the setId( ) method to assign it on line 12.

Report erratum

this copy is (P1.5 printing, July 21, 2009)
Prepared exclusively for Trieu Nguyen

Download at Boykma.Com

http://media.pragprog.com/titles/eband/code/Sudokuv4/src/org/example/sudoku/PuzzleView.java
http://books.pragprog.com/titles/eband/errata/add?pdf_page=124

Page 246

WINDOWS 246 WINDOWS

Title Year ISBN Pages

Stripes ...and Java Web Development Is Fun

Again

2008 9781934356210 375

TextMate: Power Editing for the Mac 2007 9780978739232 208

The Definitive ANTLR Reference: Building

Domain-Specific Languages

2007 9780978739256 384

The Passionate Programmer: Creating a

Remarkable Career in Software Development

2009 9781934356340 200

ThoughtWorks Anthology 2008 9781934356142 240

Ubuntu Kung Fu: Tips, Tricks, Hints, and Hacks 2008 9781934356227 400

Prepared exclusively for Trieu Nguyen

Page 247

The Pragmatic Bookshelf
The Pragmatic Bookshelf features books written by developers for developers. The titles

continue the well-known Pragmatic Programmer style and continue to garner awards and

rave reviews. As development gets more and more difficult, the Pragmatic Programmers

will be there with more titles and products to help you stay on top of your game.

Visit Us Online
Hello Android’s Home Page

http://pragprog.com/titles/eband

Source code from this book, errata, and other resources. Come give us feedback, too!

Register for Updates

http://pragprog.com/updates

Be notified when updates and new books become available.

Join the Community

http://pragprog.com/community

Read our weblogs, join our online discussions, participate in our mailing list, interact

with our wiki, and benefit from the experience of other Pragmatic Programmers.

New and Noteworthy

http://pragprog.com/news

Check out the latest pragmatic developments in the news.

Buy the Book
If you liked this PDF, perhaps you’d like to have a paper copy of the book. It’s available

for purchase at our store: pragprog.com/titles/eband.

Contact Us
Phone Orders: 1-800-699-PROG (+1 919 847 3884)

Online Orders: www.pragprog.com/catalog

Customer Service: [email protected]

Non-English Versions: [email protected]

Pragmatic Teaching: [email protected]

Author Proposals: [email protected]

Prepared exclusively for Trieu Nguyen

http://pragprog.com/titles/eband
http://pragprog.com/updates
http://pragprog.com/community
http://pragprog.com/news
pragprog.com/titles/eband
www.pragprog.com/catalog

Similer Documents