Android A programmers guide_第1页
Android A programmers guide_第2页
Android A programmers guide_第3页
Android A programmers guide_第4页
Android A programmers guide_第5页
已阅读5页,还剩332页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 androida programmers guide this page intentionally left blank androida programmers guidej.f. dimarzionew york chicago san franciscolisbon london madrid mexico citymilan new delhi san juanseoul singapore sydney toronto copyright 2008 by the mcgraw-hill companies. all rights reserved. manufactured in

2、 the united states of america. except as permittedunder the united states copyright act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, orstored in a database or retrieval system, without the prior written permission of the publisher.0-07-159989-4th

3、e material in this ebook also appears in the print version of this title: 0-07-159988-6.all trademarks are trademarks of their respective owners. rather than put a trademark symbol after every occurrence of a trademarked name,we use names in an editorial fashion only, and to the benefit of the trade

4、mark owner, with no intention of infringement of thetrademark. where such designations appear in this book, they have been printed with initial caps.mcgraw-hill ebooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate trainingprograms. for m

5、ore information, please contact george hoare, special sales, at george_hoaremcgraw- or (212) 904-4069.terms of usethis is a copyrighted work and the mcgraw-hill companies, inc. (mcgraw-hill) and its licensors reserve all rights in and to the work. useof this work is subject to these terms. except as

6、 permitted under the copyright act of 1976 and the right to store and retrieve one copy of thework, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute,disseminate, sell, publish or sublicense the work or any part of it wi

7、thout mcgraw-hills prior consent. you may use the work for your ownnoncommercial and personal use; any other use of the work is strictly prohibited. your right to use the work may be terminated if you fail tocomply with these terms.the work is provided as is. mcgraw-hill and its licensors make no gu

8、arantees or warranties as to theaccuracy, adequacy or completeness of or results to be obtained from using the work, including anyinformation that can be accessed through the work via hyperlink or otherwise, and expressly dis-claim any warranty, express or implied, including but not limited to impli

9、ed warranties of mer-chantability or fitness for a particular purpose. mcgraw-hill and its licensors do not warrant or guarantee that the func-tions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. neither mcgraw-hill nor itslicensors shall

10、 be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for anydamages resulting therefrom. mcgraw-hill has no responsibility for the content of any information accessed through the work. under nocircumstances shall mcgraw-hill and/or its licensors

11、 be liable for any indirect, incidental, special, punitive, consequential or similar damagesthat result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. thislimitation of liability shall apply to any claim or cause whatsoever whet

12、her such claim or cause arises in contract, tort or otherwise.doi: 10.1036/0071599886 professionalwant to learn more?we hope you enjoy thismcgraw-hill ebook! ifyoud like more information about this book,its author, or related books and websites,pleaseclick here. this book is dedicated to suzannah, c

13、hristian, and sophia about the authorj.f. dimarziois a developer with over 15 years ofexperience in networking and application developmentand is the author of seven books on computing technologies.he has become a leading resource in the fields of itconsulting and development. he lives in central flo

14、rida.about the technical editorgilbert l. polois a software developer with over 20years of experience working in the telecommunications,financial, and, most recently, educational industries. hehas programmed in various languages including c, c+,java, and c#.copyright 2008 by the mcgraw-hill companie

15、s. click here for terms of use. contents at a glancewhat is android? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11downloading and installing eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92downloading and installing the and

16、roid sdk . . . . . . . . . . . . . . . . . . . . . . . 213exploring the android sdk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354application: hello world! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535using the command-line tools

17、 and the android emulator . . . . . . . . . . . . 836using intents and the phone dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177lists, menus, and other views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498using the cell phones gps functional

18、ity . . . . . . . . . . . . . . . . . . . . . . . . . . . 2039using the google api with gtalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23910application: find a friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25711android sdk tool refe

19、rence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313vii this page intentionally left blank for more information about this title,click herecon

20、tentsacknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiintroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvwhat is android? . . . . . . . . . . . . . . . . . . . . . .

21、 . . . . . . . . . . . . . . . . . . . . . . . . . 11brief history of embedded device programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2open handset alliance and android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5introduction to android . .

22、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6downloading and installing eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92why eclipse? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23、 . . . . . . . . . . . . . 11downloading and installing the jre . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12downloading and installing eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18downloading and installing the androi

24、d sdk . . . . . . . . . . . . . . . . . . . . . . . 213downloading the android sdk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24downloading and installing the android plugin for eclipse . . . . . . . . . . . . . . . . . . . . . . 24configuring the android p

25、lugin for eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30ix xandroid: a programmers guideexploring the android sdk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354what is in the android sdk? . . . . . . . . . . . . . . . . . . . . . . . .

26、 . . . . . . . . . . . . . . . . . . . . . 37android documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38android samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39try this: run the api d

27、emos sample application . . . . . . . . . . . . . . . . . . . . . . . . . 41android tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45apis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28、 . . . . . . . . . . 48application life cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49standard asp application life cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49android application life cycle . . . . . . . .

29、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50application: hello world! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535creating your first android project in eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55examining the and

30、roid-created files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61androidmanifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62referenced libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31、. . . . . . . . . . . . . . . 62directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63hello world! again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69hello world! usin

32、g an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72hello world! code-based ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75hello world! xml-based ui . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33、. . . . . . . . . 78try this: use textview and imageview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81using the command-line tools and the android emulator . . . . . . . . . . . . 836creating a shell activity using the windows cli . . . . . . . . . . . . . . . . . . . . . . .

34、. . . . . . 84running the activitycreator.bat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85the project structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88creating the hello world! activity in the windows cli . .

35、. . . . . . . . . . . . . . . . . . . . . . 95editing the project files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95adding the java_home variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96compiling and installing the appli

36、cation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97hello world! on linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109configuring the path statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37、 109try this: create an image-based hello world! in the cli . . . . . . . . . . . . . . . . . . . 115using intents and the phone dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177what are intents? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38、. . . . . . . . . . . . . . . . 119using the dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124placing a call from your activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128adding the i

39、ntent to your activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129editing activity permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 xicontentsmodifying the androidphonedialer . . . . . . . . . . . . . . . . . . . . . .

40、. . . . . . . . . . . . . . . . . . 136adding a button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136implementing an edittext view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141try this: modify the androidpho

41、nedialer project . . . . . . . . . . . . . . . . . . . . . . . . . . 145lists, menus, and other views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1498building the activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42、. . 151intent code for the .xml file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152intent code for the .java file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154modifying the androidmanifest.xml . . . . . . . . . . . . . .

43、. . . . . . . . . . . . . . . . . . . . . 155using the menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157creating the activity for autocomplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163button . . . . . .

44、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173checkbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178edittext . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

45、. . . . . . . . . . . . . . . . . . . . . . . . . . . . 183radiogroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189spinner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46、 . . 195try this: modify more view attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202using the cell phones gps functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 2039using the android location-based api . . . . . . . . . . . . . . . . . . . . . . . .

47、. . . . . . . . . . . . . 204creating a kml file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205what is a track file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208getting the nmea file in window

48、s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208getting the nmea file in linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210reading the gps with the android location-based api . . . . . . . . . . . . . . . . . . . . . . . . 212creating

49、 the androidlbs activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212passing coordinates to google maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222adding zoom controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50、 . . . . . . . . 226try this: toggling between mapviews standard and satellite views . . . . . . . . . . 232using the google api with gtalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23910configuring the android emulator for gtalk . . . . . . . . . . . . . . . . . . . . . .

51、. . . . . . . . . . . 241implementing gtalk in android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244creating the activitys layout in the googleapi.xml . . . . . . . . . . . . . . . . . . . . . 245adding packages to googleapi.java . . . . . . . . . . . . .

52、. . . . . . . . . . . . . . . . . . . . . . 247implementing the view.onclicklistener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248compiling and running googleapi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252try this: add a settings feature to your goog

53、leapi activity . . . . . . . . . . . . . . . . 255 xiiandroid: a programmers guideapplication: find a friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25711creating a sqlite database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54、 . . . . . . . 259creating a custom content provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263editing the strings.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263creating your content provider . . . . . . .

55、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265creating the findafriend activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276editing androidmanifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276creat

56、ing the nameeditor activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278creating the locationeditor activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283creating the friendsmap activity . . . . . . . . . . . . . . . . . . . . . . . . . .

57、. . . . . . . . . . . 293creating the findafriend activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299running the findafriend activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302try this: real-time location updating . . . . .

58、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305android sdk tool reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712android emulator commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308android debug bridge commands . . . . . . . . . . . . . . . . . . . . . . . . . .

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论