Busy Java Developer's Guide to Android

Arya MirΚινητά – Ασύρματες Τεχνολογίες

3 Οκτ 2011 (πριν από 6 χρόνια και 22 μέρες)

866 εμφανίσεις

Ted Neward http://www.tedneward.com

Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

1

Busy  Java  Developer's  Guide
 
to  Android
 
 
Ted  Neward
 
http://www.tedneward.com  
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

2

Credentials
 

Who  is  this  guy?
 
ʹ
Principal  Consultant:  architect,  mentor,  free  agent  coach
 
ʹ
BEA  Technical  Director,  Microsoft  MVP  Architect
 
ʹ
JSR  175,  250,  277  EG  member
 
ʹ
Founding  Editor
-­‐
in
-­‐
Chief:  TheServerSide.NET
 
ʹ
Author
 

 
Professional  F#  
 
(Forthcoming)
 

 
Effective  Enterprise  Java
 
(Addison
-­‐
Wesley,  2004)
 

 
Server
-­‐
Based  Java  Programming
 
(Manning,  2000)
 

 
C#  in  a  Nutshell    
(OReilly,  2003)
 

 
SSCLI  Essentials  
(w/Stutz,  Shilling;  OReilly,  2003)
 
ʹ
Papers  at  
http://www.tedneward.com
 
 
ʹ
Weblog  at  
http://blogs.tedneward.com
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

3

Objectives
 

DũŽď
 
ʹ
ŝŽŚŽŽĞŶŽŐŚŶĚŽŝĚŽŵĂŬĞŽĚĂŶŐĞŽ
 
ʹ
ďĞĐĂĞ/ĐĂŶĞŚĂŝĞůĐŽĞŚĞĞŶŝĞůĂĨŽŵŝŶ
just  one  conference  session
 

dŽŚĂĞŶĚ
 
ʹ
/ŝůůŚŽŽŚĞ;ďĂĞďŽŶĞŽŽů
 
ʹ
/ŝůůŚŽŽŽŵĞďĂŝĐ
 
ʹ
/ŝůůĞůĂŝŶĂĨĞĐŽĞĐŽŶĐĞ
 
ʹ
/ŝůůůĞĂĞŝŽŽŽĞĞĂĐŚĨŚĞ
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

4

Concepts
 

Activities
 

Content  Providers
 

Services
 

Intents
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

5

Features
 

Storage  (SD  cards)
 

Network
 

Camera/Video
 

OpenGL
 

Sound/Music
 

GPS
 

Phone/SMS
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

6

Tools
 

Android  tooling  consists  of:
 
ʹ
JDK  1.6.latest
 
ʹ
Android  SDK
 
Android  SDK  installer/updater
 
Android  libraries  &  documentation  (versioned)
 
Android  emulator
 
ADB
 
ʹ
an  Android  device  (optional,  sort  of)
 
ŵŝŶĞŝĂDŽŽŽůĂŽŝĚ;ĨŽŶŽ
 
ʹ
Eclipse  w/Android  plugins  (optional)
 
/ĚŽŶĂŝĐůĂůĐĂĞĨŽŝ
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

7

Bits
 

Android  is  not  a  JVM
 
ʹ
Android  is  a  mix  of  native  and  VM  (Dalvik)  code
 
ʹ
Runs  on  top  of  a  specialized  build  of  Linux
 
ʹ
Java  source  is  compiled  to  JVM  bytecode
 
ʹ
Postprocessor  converts  JVM  bytecode  to  DVM  bytecode
 
ʹ
All  code  and  files  are  packaged  up  (APK  files)
 
ʹ
APK  downloaded  to  phone,  installed,  and  ready
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

8

Getting  Started
 

KŶĐĞŚĞŽŽůĂĞŝŶĂůůĞĚ
 
ʹ
ĂŶĚŽŝĚŽŶŽĐŽŵŵĂŶĚ
-­‐
line  PATH
 
ʹ
create  a  new  AVD  (Android  Virtual  Machine)
 
android  create  avd  
-­‐-­‐
target  7  
-­‐-­‐
name  TestAVD
 
ʹ
create  a  new  Android  project
 
android  create  project  
-­‐-­‐
target  7  
-­‐-­‐
path  ./HelloAndroid  
-­‐-­‐
activity  HelloActivity  
-­‐-­‐
package  com.tedneward.tutorial
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

9

Getting  Started
 

Project  contents
 
ʹ
build.xml:  Ant  build  script  integrated  with  Android  SDK
 
ʹ
build.properties,  local.properties:  settings  for  build
 
ʹ
AndroidManifest.xml:  Android  manifest  file  (more  later)
 
ʹ
assets/:  static  files  (for  packaging  in  the  app)
 
ʹ
bin/:  generated  binaries  (including  the  APK  file)
 
ʹ
gen/:  generated  source  files
 
ʹ
libs/:  Java  jars  used  in  your  app
 
ʹ
src/:  your  app  source
 
ʹ
res/:  resources  used  in  the  app
 
ʹ
tests/:  unit
-­‐
tests
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

10

Getting  Started
 

Resources  (res/  directory)
 
ʹ
res/drawable/  :  graphics  (PNG,  JPEG,  etc)
 
ʹ
res/layout/  :  UI  XML  layout  files
 
ʹ
res/menu/  :  UI  menu  XML  files
 
ʹ
ĞĂŐĞŶĞĂů
-­‐
ŽĞĨŝůĞ;ĞŐĂŶŚŝŶŐĞůĞ
 
ʹ
res/values/  :  general  constants
 
ʹ
res/xml/  :  any  other  XML  files  included  in  the  build
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

11

Getting  Started
 

AndroidManifest.xml:  application  manifest
 
ʹ
uses
-­‐
permission:  what  does  the  app  do?
 
ʹ
permissions:  what  new  permissions  does  the  app  define?
 
ʹ
instrumentation:  what  events  should  be  hooked?
 
ʹ
uses
-­‐
library:  which  optional  components  does  app  use?
 
ʹ
uses
-­‐
sdk:  which  SDK  was/is  used?
 
ʹ
application:  what  are  the  contents  in  the  app?
 
activity:  defines  an  activity
 
service:  defines  a  service
 
provider:  defines  a  content  provider
 
receiver:  defines  a  broadcast  receiver
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

12

UI  Basics
 

Activity
 
ʹ
somewhat  akin  to  a  web  page  or  screen
 
ʹ
public  class  that  extends  android.app.Activity
 
ʹ
ĂĐŝŝŝĞĨŽŵĂĂĐŬŽĨĐĂĚĂŚĞĞŵŽĞĂŽŶĚ
 
ďĂĐŬďŽŶŽŶŚĞŚĂŶĚĞĞŵŽĞŚĞŽĐĂĚ
 
ŐŽŝŶŐďĂĐŬĚŽĞŶŶĞĐĞĂŝůŬŝůůŚĞĂĐŝŝŚŽŐŚ
 
ʹ
activities  have  event  methods  for  override
 
ŽŶĞĂĞĂĐŝŝĐŽŶĐŽ
 
ŽŶĞŽĂĐŝŝĨŝŶĂůŝĞ
 
onStart/onStop/onPause/onResume/onRestart
 
remember  to  call  up  the  inheritance  chain
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

13

UI  Basics
 

Controls
 
ʹ
Label:  text  (readonly)  label
 
ʹ
Button,  Checkbox,  Radiobutton
 
ʹ
Image
 
ʹ
more  (listboxes,  etc)
 

Control  creation
 
ʹ
ĞŝŚĞĐĞĂĞĐŽŶŽůďŚĂŶĚŝŶ:ĂĂ
 
ʹ
or  using  XML
-­‐
based  layout  file
 
ʹ
pros/cons  either  way
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

14

UI  Basics
 

Moving  from  one  Activity  to  another
 
ʹ
ĞŝĞĂŶŝŶĞŶŽďĞĂĞĚŽŚĞŶĚŽŝĚK^ĨŽ
processing  and  evaluation
 
ʹ
Intent  =  Action  +  Context
 
ʹ
ĞĂŝĞ/ŶĞŶŝŚĞůĂŶĐŚŚĞĐŝŝ/ŶĞŶ
 
Intent  next  =  new  Intent(this,  NextActivity.class);
 
startActivity(next);
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

15

UI  Basics
 

Threading
 
ʹ
This  is  a  phone

ŽĚŽŶŽŶŚĞŵĂĐŚŝŶĞ
 
ʹ
ŶĚŽŝĚŝŶŝĞůŝŬĞ:ĂĂĂŶĚŚĂĂůŝŐŚůĚŝĨĨĞĞŶ
ĂŬĞŽŶŚĞŚĞĂĚ
-­‐
and
-­‐
UI  position
 
NO  UI  modifications  from  non
-­‐
UI  thread
 
If  the  UI  thread  is  inactive  for  more  than  5  seconds,  bye!
 
ʹ
Android  provides  Handlers,  which  can  be  sent  Messages  
ŚĂŝůůŚĞŶďĞŽĐĞĞĚŽŶŚĞĐŝŝh/ŚĞĂĚ
 
ʹ
Java5  provides  a  few  other  constructs  as  well
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

16

Summary
 

Android  is  a  Java
-­‐
based  mobile  device  framework
 
ʹ
ďŝŶŽ:ĂĂ
 
ʹ
ĂŶĚŝĚĞĨŝŶŝĞůŶŽ^ŝŶŐŽ^td
 
ʹ
ďŝĚŽĞůĞŽĞŚĞ:ĂĂĂŽůŽĞ
 
ʹ
ŽůŽŶŐĂŚĞĨŝĂ;ĞůĂŝĞůŶĂŽŽĨŝůĞ
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

17

References
 

ŽĚĞ'ŝĚĞŽŶĚŽŝĚ
 
ʹ
Mark  Murphy,  Commonsware,  2009
 

Android  website  
http://www.android.com
 
 
 
Neward & Associates

© 2006 Neward & Associates. All rights reserved.

This presentation is for informational purposes only.

18

Questions
 
?