Saturday, August 20, 2011

Learning Android






Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Android Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Android Overview 1
Comprehensive 1
Open Source Platform 2
Designed for Mobile Devices 2
History 3
Google’s Motivation 3
Open Handset Alliance 4
Android Versions 4
Summary 5
2. The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Stack Overview 7
Linux 7
Portability 7
Security 8
Features 8
Native Libraries 9
Dalvik 9
Android and Java 10
Application Framework 11
Applications 12
The APK 12
Application Signing 12
Application Distribution 12
Summary 13
3. Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Installing the Android SDK 15
Setting Up a PATH to Tools 16
Installing Eclipse 16
Eclipse Workspace 17
Setting Up Android Development Tools 17
Hello, World 18
Creating a New Project 18
Manifest File 20
Layout XML Code 21
Strings 21
The R File 22
Java Source Code 22
The Emulator 23
An Emulator Versus a Physical Phone 25
Summary 25
4. Main Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
What Are Main Building Blocks? 27
A Real-World Example 27
Activities 28
Activity Life Cycle 28
Intents 31
Services 31
Content Providers 32
Broadcast Receivers 34
Application Context 34
Summary 35
5. Yamba Project Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The Yamba Application 37
Design Philosophy 39
Project Design 39
Part 1: Android User Interface 39
Building an Activity 40
Networking and Multithreading 41
Debugging Android Apps 41
Part 2: Preferences, Filesystem, Options Menu, and Intents 41
The Activity 41
Menu System and Intents 42
Filesystem 42
Part 3: Android Services 42
Services 42
Application Object 42
Part 4: Working with Databases 42
SQLite and Android’s Support for It 42
Refactoring the Code Again 43
Part 5: Lists and Adapters 43
Timeline Activity 43
More Refactoring? 43
Part 6: Broadcast Receivers 43
Boot and Network Receivers 44
Timeline Receiver 44
Permissions 44
Part 7: Content Providers 44
Status Data 44
Android Widgets 44
Part 8: System Services 45
Compass and Location 45
Intent Service, Alarms, and Notifications 45
Summary 45
6. Android User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Two Ways to Create a User Interface 47
Declarative User Interface 47
Programmatic User Interface 48
The Best of Both Worlds 48
Views and Layouts 48
LinearLayout 49
TableLayout 50
FrameLayout 50
RelativeLayout 50
AbsoluteLayout 50
Starting the Yamba Project 51
The StatusActivity Layout 52
Imprtant Widget Properties 54
Strings Resource 55
The StatusActivity Java Class 56
Creating Your Application-Specific Object and Initialization Code 56
Compiling Code and Building Your Projects: Saving Files 59
Adding the jtwitter.jar Library 59
Updating the Manifest File for Internet Permission 61
Logging in Android 62
LogCat 62
Threading in Android 65
Single Thread 65
Multithreaded Execution 66
AsyncTask 67
Other UI Events 70
Adding Color and Graphics 74
Adding Images 74
Adding Color 76
Alternative Resources 79
Optimizing the User Interface 80
Hierarchy Viewer 81
Summary 82
7. Preferences, the Filesystem, the Options Menu, and Intents . . . . . . . . . . . . . . . . . . 83
Preferences 83
Prefs Resource 84
PrefsActivity 87
Update the Manifest File 88
The Options Menu 89
The Menu Resource 89
Android System Resources 90
Update StatusActivity to Load the Menu 91
Update StatusActivity to Handle Menu Events 92
Strings Resource 92
Shared Preferences 93
The Filesystem Explained 95
Exploring the Filesystem 95
Filesystem Partitions 96
System Partition 96
SDCard Partition 96
The User Data Partition 97
Filesystem Security 98
Summary 99
8. Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Yamba Application Object 102
The YambaApplication Class 102
Update the Manifest File 104
Simplifying StatusActivity 105
UpdaterService 105
Creating the UpdaterService Java Class 106
Update the Manifest File 107
Add Menu Items 108
Update the Options Menu Handling 109
Testing the Service 109
Looping in the Service 110
Testing the Service 113
Pulling Data from Twitter 113
Testing the Service 117
Summary 117
9. The Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
About SQLite 119
DbHelper 120
The Database Schema and Its Creation 120
Four Major Operations 121
Cursors 122
First Example 122
Update UpdaterService 124
Testing the Service 127
Database Constraints 129
Refactoring Status Data 130
Summary 135
10. Lists and Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
TimelineActivity 137
Basic TimelineActivity Layout 138
Introducing ScrollView 138
Creating the TimelineActivity Class 139
About Adapters 142
Adding a ListView to TimelineActivity 142
Creating a Row Layout 143
Creating an Adapter in TimelineActivity.java 144
TimelineAdapter 146
ViewBinder: A Better Alternative to TimelineAdapter 149
Updating the Manifest File 150
Initial App Setup 152
Base Activity 153
Toggle Service 154
Summary 159
11. Broadcast Receivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
About Broadcast Receivers 161
BootReceiver 162
Registering the BootReceiver with the AndroidManifest File 162
Testing the Boot Receiver 163
The TimelineReceiver 163
Broadcasting Intents 165
The Network Receiver 167
Adding Custom Permissions to Send and Receive Broadcasts 169
Declaring Permissions in the Manifest File 170
Updating the Services to Enforce Permissions 171
Updating TimelineReceiver to Enforce Permissions 172
Summary 173
12. Content Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Creating a Content Provider 175
Defining the URI 176
Inserting Data 177
Updating Data 178
Deleting Data 179
Querying Data 179
Getting the Data Type 180
Updating the Android Manifest File 181
Using Content Providers Through Widgets 181
Implementing the YambaWidget class 182
Creating the XML Layout 185
Creating the AppWidgetProviderInfo File 185
Updating the Manifest File 186
Testing the Widget 186
Summary 186
13. System Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Compass Demo 189
Common Steps in Using System Services 190
Getting Updates from the Compass 190
Compass Main Activity 191
Custom Rose Widget 194
Location Service 195
Where Am I? Demo 196
Updating Yamba to Use the Location Service 200
Updating Our Preferences 200
Updating the Yamba Application 201
Updating the Status Activity 202
Intent Service 206
Alarms 208
Adding an Interval to Preferences 209
Updating BootReceiver 210
Sending Notifications 212
Summary 214
14. The Android Interface Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Implementing the Remote Service 215
Writing the AIDL 216
Implementing the Service 217
Implementing a Parcel 218
Registering with the Manifest File 220
Implementing the Remote Client 221
Binding to the Remote Service 221
Testing That It All Works 224
Summary 225
15. The Native Development Kit (NDK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
What Is and Isn’t the NDK For? 227
Problems Solved by the NDK 227
The Toolchain 228
Packaging Your Libs 228
Documentation and Standardized Headers 228
An NDK Example: Fibonacci 229
FibLib 229
The JNI Header File 231
C Implementation 232
The Makefile 234
Building the Shared Library 234
The Fibonacci Activity 235
Testing That It All Works 236
Summary 237
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Another Android Books
Download

No comments:

Post a Comment

Related Posts with Thumbnails

Put Your Ads Here!