Unlocking Android

Unlocking Android

Unlocking Frank Ableson Charlie Collins Robi Sen FOREWORD BY DICK WALL MANNING Unlocking Android Licensed to Deborah Christiansen <[email protected]> Licensed to Deborah Christiansen <[email protected]> Unlocking Android A DEVELOPER’S GUIDE W. FRANK ABLESON CHARLIE COLLINS ROBI SEN MANNING Greenwich (74° w. long.) Licensed to Deborah Christiansen <[email protected]> To Nikki —W.F.A. For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] ©2009 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine. Development Editor Tom Cirtin Manning Publications Co. Copyeditor: Linda Recktenwald Sound View Court 3B Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 978-1-933988-67-2 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 14 13 12 11 10 09 Licensed to Deborah Christiansen <[email protected]> brief contents PART 1 WHAT IS ANDROID? — THE BIG PICTURE.............................. 1 1 ■ Targeting Android 3 2 ■ Development environment 32 PART 2 EXERCISING THE ANDROID SDK ......................................... 57 3 ■ User interfaces 59 4 ■ Intents and services 97 5 ■ Storing and retrieving data 126 6 ■ Networking and web services 167 7 ■ Telephony 195 8 ■ Notifications and alarms 211 9 ■ Graphics and animation 226 10 ■ Multimedia 251 11 ■ Location, location, location 266 PART 3 ANDROID APPLICATIONS................................................... 293 12 ■ Putting it all together–the Field Service Application 295 13 ■ Hacking Android 341 v Licensed to Deborah Christiansen <[email protected]> Licensed to Deborah Christiansen <[email protected]> contents foreword xiii preface xv acknowledgments xvii about this book xx about the cover illustration xxiv PART 1WHAT IS ANDROID? — THE BIG PICTURE .............. 1 Targeting Android 3 1 1.1 Introducing Android 4 The Android platform 4 ■ In the market for an Android? 6 Licensing Android 10 1.2 Stacking up Android 11 Probing Android’s foundation 12 1.3 Booting Android development 14 Android’s good Intent-ions 14 ■ Activating Android 18 AndroidManifest.xml 25 ■ Mapping applications to processes 26 1.4 An Android application 27 1.5 Summary 30 vii Licensed to Deborah Christiansen <[email protected]> viii CONTENTS Development environment 32 2 2.1 The Android SDK 33 The application programming interface 33 ■ Core Android packages 33 Optional packages 34 2.2 Fitting the pieces together 35 Java Perspective 36 ■ DDMS Perspective 38 ■ Command-Line tools 40 2.3 Building an Android application in Eclipse 42 Android Project Wizard 43 ■ Android sample application code 43 Building the application 48 2.4 The Android Emulator 50 Skins 50 ■ Network speed 51 ■ Emulator profiles 53 2.5 Debugging 55 2.6 Summary 56 PART 2EXERCISING THE ANDROID SDK...........................57 User interfaces 59 3 3.1 Creating the Activity 60 Creating an Activity class 62 ■ Exploring Activity lifecycle 67 3.2 Working with views 70 Exploring common views 71 ■ Using a ListView 73 ■ Multitasking with Handler and Message 77 ■ Creating custom views 78 Understanding layout 80 ■ Handling focus 82 ■ Grasping events 83 3.3 Using resources 84 Supported resource types 85 ■ Referencing resources in Java 85 Defining views and layouts through XML resources 87 Externalizing values 89 ■ Providing animations 92 3.4 Understanding the AndroidManifest file 93 3.5 Summary 95 Intents and services 97 4 4.1 Working with Intent classes 98 Defining intents 99 ■ Intent resolution 102 ■ Matching a custom URI 105 ■ Using Android-provided activities 109 4.2 Listening in with broadcast receivers 110 Overloading the Intent concept 110 ■ Creating a receiver 112 Licensed to Deborah Christiansen <[email protected]> CONTENTS ix 4.3 Building a Service 113 Dual-purpose nature of a Service 113 ■ Creating a background task Service 114 4.4 Performing Inter-Process Communication 117 Android Interface Definition Language 117 ■ Exposing a remote interface 120 ■ Binding to a Service 120 ■ Starting versus binding 122 ■ Service lifecycle 123 ■ Binder and Parcelable 124 4.5 Summary 125 Storing and retrieving data 126 5 5.1 Using preferences 127 Working with SharedPreferences 127 ■ Preference access permissions 130 5.2 Using the filesystem 134 Creating files 134 ■ Accessing files 135 ■ Files as raw resources 136 ■ XML file resources 137 ■ External storage via an SD card 139 5.3 Persisting data to a database 143 Building and accessing a database 143 ■ Using the sqlite3 tool 148 5.4 Working with ContentProvider classes 149 Understanding URI representations and manipulating records 151 Creating a ContentProvider 158 5.5 Summary 165 Networking and web services 167 6 6.1 An overview of networking 169 Networking basics 169 ■ Clients and servers 171 6.2 Checking the network status 172 6.3 Communicating with a server socket 173 6.4 Working with HTTP 176 Simple HTTP and java.net 177 ■ Robust HTTP with HttpClient 179 Creating an HTTP and HTTPS helper 181 6.5 Web services 186 POX—Putting it together with HTTP and XML 187 ■ REST 189 To SOAP or not to SOAP, that is the question 193 6.6 Summary 194 Licensed to Deborah Christiansen <[email protected]> x CONTENTS Telephony 195 7 7.1 Telephony background and terms 197 7.2 Accessing telephony information 198 Retrieving telephony properties 198 ■ Obtaining phone state information 200 7.3 Interacting with the phone 202 Using intents to make calls 202 ■ Helpful phone number–related utilities 204 ■ Intercepting calls 205 7.4 Working with messaging: SMS 206 Sending SMS messages 207 ■ Receiving SMS messages 209 7.5 Summary 210 Notifications and alarms 211 8 8.1 Introducing Toast 212 8.2 Introducing notifications 215 8.3 Alarms 219 Alarm example 219 8.4 Summary 225 Graphics and animation 226 9 9.1 Drawing graphics in Android 226 Drawing with XML 228 9.2 Animations 231 Programmatically creating an animation 233 ■ Introducing OpenGL for embedded systems 237 9.3 Summary 250 Multimedia 251 10 10.1 Introduction to multimedia and OpenCORE 252 10.2 Playing audio 253 10.3 Playing video 254 10.4 Capturing media 257 Understanding the camera 257 ■ Capturing audio 262 10.5 Summary 265 Licensed to Deborah Christiansen <[email protected]> CONTENTS xi Location, location, location 266 11 11.1 Simulating your location within the emulator 268 Sending in your coordinates with the DDMS tool 268 ■ The GPS Exchange Format 270 ■ The Google Earth Keyhole Markup Language 273 11.2 Using LocationManager and LocationProvider 274 Accessing location data with LocationManager 275 ■ Using a LocationProvider 277 ■ Receiving location updates with LocationListener 279 11.3 Working with maps 281 Extending MapActivity 282 ■ Using a MapView 283 ■ Placing data on a map with an Overlay 285 11.4 Converting places and addresses with Geocoder 289 11.5 Summary 291 PART 3ANDROID APPLICATIONS ................................... 293 Putting it all together–the Field Service Application 295 12 12.1 Field Service Application requirements 296 Basic requirements 297 ■ Data model 298 ■ Application architecture and integration 299 12.2 Android application tour 300 Application flow 300 ■ Code road map 302 AndroidManifest.xml 303 12.3 Android code 304 Splash Activity 304 ■ FieldService Activity, part 1 306 ■ FieldService Activity, part 2 308 ■ Settings 309 ■ Data structures 311 12.4 Digging deeper into the code 319 RefreshJobs 319 ■ ManageJobs 323 ■ ShowJob 325 ■ CloseJob 329 12.5 Server code 336 Dispatcher user interface 336 ■ Database 337 ■ PHP dispatcher code 337 ■ PHP mobile integration code 338 12.6 Summary 339 Licensed to Deborah Christiansen <[email protected]> xii CONTENTS Hacking Android 341 13 13.1 The Android/Linux:junction 342 Tool chain 342 ■ Building an application 343 ■ Installing and running the application 344 ■ Build script 346 13.2 A better way 347 The static flag, revisited 347 ■ Linking 349 ■ Exit, not return 351 Startup code 352 13.3 What time is it? 355 Daytime Server application 355 ■ daytime.c 355 ■ The SQLite database 358 ■ Building and running Daytime Server 360 13.4 Daytime Client 362 Activity 362 ■ Socket client 363 ■ Testing Daytime Client 364 13.5 Summary 365 appendix A Installing the Android SDK 367 appendix B Signing and installing applications on an Android device 375 index 383 Licensed to Deborah Christiansen <[email protected]> foreword The mobile phone and portable device handset are currently undergoing a transfor- mation caused by several different factors. For one, portable devices are getting more powerful and capable of performing tasks that would have been hard to imagine a few short years ago. Many of us carry a portable device that is capable of everything from using the World Wide Web to watching movies to playing 3D games--and it can even make phone calls! For another, consumers are becoming more savvy and demanding about what they want such a device to do. A third part of the convergence is that por- table devices now form a bigger market for software and applications developers than larger computing platforms, and delivery of applications to those devices is often eas- ier and more streamlined than to larger ones.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    418 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us