Top Banner
Android Overview Most of the material in this sec5on comes from h6p://developer.android.com/guide/
63

Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Oct 07, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Overview  Most  of  the  material  in  this  sec5on  comes  from  

h6p://developer.android.com/guide/  

Page 2: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

 

Android  Overview  

•  A  so=ware  stack  for  mobile  devices  

•  Developed  and  managed  by  Open  Handset  Alliance  

•  Open-­‐sourced  under  Apache  License  

Key  Applica5ons  

Middleware  Opera5ng  System  (Linux  Kernel  2.6)  

AAD Hands-On (Muppala) Android Overview 2

Page 3: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Features  •  Applica'on  framework  enabling  reuse  and  replacement  of  components  •  Dalvik  virtual  machine  op5mized  for  mobile  devices  •  Integrated  browser  based  on  the  open  source  WebKit  engine  •  Op'mized  graphics  powered  by  a  custom  2D  graphics  library;  3D  graphics  

based  on  the  OpenGL  ES  1.0  specifica5on  (hardware  accelera5on  op5onal)  •  SQLite  for  structured  data  storage  •  Media  support  for  common  audio,  video,  and  s5ll  image  formats  (MPEG4,  

H.264,  MP3,  AAC,  AMR,  JPG,  PNG,  GIF)  •  GSM  Telephony  (hardware  dependent)  •  Bluetooth,  EDGE,  3G,  and  WiFi  (hardware  dependent)  •  Camera,  GPS,  compass,  and  accelerometer  (hardware  dependent)  •  Rich  development  environment  including  a  device  emulator,  tools  for  

debugging,  memory  and  performance  profiling,  and  a  plugin  for  the  Eclipse  IDE  

Source:  h6p://developer.android.com  

AAD Hands-On (Muppala) Android Overview 3

Page 4: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Open  Handset  Alliance  (OHA)

•  Open  Handset  Alliance™,  a  group  of  80  technology  and  mobile  companies

AAD Hands-On (Muppala) Android Overview 4

Open  Handset  Alliance  

Mobile  Operators  

Handset  Manufacturers  

Semiconductor  Companies  

So=ware  Companies  

Commercializa5on  Companies  

Page 5: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Pla`orm  Versions  

•  Moving  target,  new  pla`orms  released  regularly  

AAD Hands-On (Muppala) Android Overview 5

Name   Version   API  Level   Release  Date   Linux  Kernel  Base   1.0   1   Sep.  2008  Base_1_1   1.1   2   Feb.  2009  Cupcake   1.5   3   Apr.  2009   2.6.27  Donut   1.6   4   Sep.2009   2.6.29  Éclair   2.0   Oct.  2009   2.6.29  

2.1-­‐update  1   7   Jan.  2010   2.6.29  Froyo   2.2   8   May  2010   2.6.32  

Gingerbread  2.3/2.3.1/  2.3.2   9   Dec.  2010   2.6.35  

2.3.3/2.3.4   10   Jul.  2011   2.6.35  

Honeycomb  3.0.x   11   Feb.  2011  3.1.x   12   May  2011  3.2.x   13   Jul.  2011  

Ice  Cream  Sandwich  

4.0/4.0.1/4.0.2   14   Dec.  2011  

4.0.3   15   Jan.  2012  

Page 6: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Pla`orm  Versions  as  of  Feb.  1,  2012  

AAD Hands-On (Muppala) Android Overview 6

Page 7: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture

AAD Hands-On (Muppala) Android Overview 7

Page 8: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  -­‐  Applica5ons

•  Android  ships  with  a  set  of  core  applica5ons:  –  Email  Client  –  SMS  Program  –  Calendar  –  Maps  –  Browser  –  Contacts  

•  All  applica5ons  are  wri6en  using  the  Java  language.

AAD Hands-On (Muppala) Android Overview 8

Page 9: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  –  Applica5on  Framework

•  Enabling  and  simplifying  the  reuse  of  components  –  Developers  have  full  access  to  the  same  framework  APIs  used  by  the  

core  applica5ons.  –  Applica5on  architecture  designed  to  simplify  reuse  of  components  –  Users  are  allowed  to  replace  components.  

AAD Hands-On (Muppala) Android Overview 9

Page 10: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  –  Applica5on  Framework •  Set  of  services  and  features  

AAD Hands-On (Muppala) Android Overview 10

Feature Role View    System

Used  to  build  an  applica5on,  including  lists,  grids,  text  boxes,  bu6ons,  and  embedded  web  browser

Content    Provider

Enabling  applica5ons  to  access  data  from  other    applica5ons  or  to  share  their  own  data

Resource    Manager

Providing  access  to  non-­‐code  resources  such  as  localized  strings,    graphics,  and  layout  files

No5fica5on  Manager

Enabling  all  applica5ons  to  display  custom  alerts  in  the    status  bar

Ac5vity    Manager

Manages  the  lifecycle  of  applica5ons  and  provides    a  common  naviga5on  backstack

Page 11: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  -­‐  Libraries

•  Includes  a  set  of  C/C++  libraries  used  by  various  components  of  the  Android  system  

•  Exposed  to  developers  through  the  Android  applica5on  framework  

•  Features  –  System  C  Library  (Bionic)  a  

version  of  libc  –  Media  Libraries  suppor5ng  

playback  and  recording  of  popular  audio/video  formats  

–  Surface  Manager  (Surface  Flinger)  

–  Audio  Manager  (Audio  Flinger)  

–  LibWebCore  (WebKit)  –  SGL  –  3D  Libraries:  Open  GL  ES  –  WebKit  browser  support  –  SQLite  

AAD Hands-On (Muppala) Android Overview 11

Page 12: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  -­‐  Run5me

•  Core  Libraries  –  Provides  most  of  the  func5onality  available  in  the  core  libraries  of  the  

Java  programming  language  –  APIs  

•  Data  Structures  •  U5li5es  •  File  Access  •  Network  Access  •  Graphics  •  Etc  

AAD Hands-On (Muppala) Android Overview 12

Page 13: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  –  Run5me  (Contd.)  

•  Every  Android  applica5on  runs  in  its  own  process,  with  its  own  instance  of  the  Dalvik  Virtual  Machine  

•  Dalvik  Virtual  Machine  –  Dalvik  VM  has  been  wri6en  so  that  a  device  can  run  mul5ple  VMs  

efficiently  –  Register-­‐based  virtual  machine  

AAD Hands-On (Muppala) Android Overview 13

Page 14: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  –  Run5me  (Contd.)  

•  Dalvik  Virtual  Machine  (Cont)  –  Executes  files  in  the  Dalvik  Executable  (.dex)  format,  which  is  op5mized  for  minimal  memory  footprint  

–  Runs  classes  compiled  by  a  Java  language  compiler  transformed  into  the  .dex  format  

–  Relies  on  the  Linux  kernel  for  underlying  func5onality  such  as  threading  and  low-­‐level  memory  management

a.java   Java  compiler   a.class   Dx  tool   a.dex  

AAD Hands-On (Muppala) Android Overview 14

Page 15: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Architecture  –  Linux  Kernel

•  Android  relies  on  Linux  Kernel  2.6  for  core  system  services  –  Memory  Management  –  Process  Management  –  Network  Stack  –  Driver  Model  –  Security  

•  The  Kernel  acts  as  an  abstrac5on  layer  between  the  hardware  and  the  rest  of  the  so=ware  stack  

AAD Hands-On (Muppala) Android Overview 15

Page 16: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Applica5on  Fundamentals  

Page 17: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Applica5ons

•  Android  applica5ons  are  wri6en  in  Java  •  The  compiled  Java  code  (along  with  any  data  and  resource  

files  required  by  the  applica5on)  is  bundled  by  the  aapt  tool  into  an  Android  package  (.apk)

AAD Hands-On (Muppala) Android Overview 17

.apk  package  

Java  classes  

Data  files  

Resources  

Page 18: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Android  Applica5ons  

•  Each  Android  applica5on  lives  in  its  own  world:  –  Runs  in  its  own  Linux  process  

•  Started  when  any  of  the  applica5on  code  needs  to  be  executed  •  Shuts  down  when  no  longer  needed  or  system  resources  are  required  by  other  applica5ons  

–  Each  process  has  its  own  virtual  machine  (sandboxing)  •  Applica5on  code  runs  in  isola5on  from  all  other  applica5ons  

–  Each  applica5on  assigned  a  unique  Linux  user  ID  

AAD Hands-On (Muppala) Android Overview 18

Page 19: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Applica5on  Components

•  One  applica5on  can  make  use  of  elements  of  other  applica5ons  –  Applica5ons  are  composed  of  parts  (components)  that  can  be  started  

up  when  the  need  arises,  and  instan5ate  the  Java  objects  for  that  part  –  Android  applica5ons  do  not  have  a  single  entry  point  (e.g.  no  main()  

func5on).  –  Essen5al  components  that  the  system  can  instan5ate  and  run  as  

needed  

AAD Hands-On (Muppala) Android Overview 19

Page 20: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Applica5on  Components  

•  Four  basic  components:  Ac5vi5es,  Services,  Broadcast  Receivers,  Content  Providers  

Ac5vi5es   Services  

Broadcast  Receivers   Content  Providers  

Applica5on  Components  

AAD Hands-On (Muppala) Android Overview 20

Page 21: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity

•  An  ac5vity  is  visual  UI  for  one  endeavour  the  user  can  undertake: –  Extends  the  Activity  base  class –  Displays  user  interface  controls  (views) –  Provides  for  user  interac5on

AAD Hands-On (Muppala) Android Overview 21

Page 22: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  (Contd.)

•  An  applica5on  typically  consists  of  one  or  more  ac5vi5es: –  Each  ac5vity  has  a  default  window  to  draw  in  (the  UI)  –  One  ac5vity  usually  marked  as  the  main  ac5vity  (started  when  the  

applica5on  begins) –  Moving  from  one  ac5vity  to  another  is  accomplished  by  having  the  

current  ac5vity  start  the  next  one  –  An  ac5vity  may  return  a  result  to  the  previous  ac5vity.

•  Details  of  ac5vi5es  specified  in  the  Applica5on’s  Android  Manifest  file  

AAD Hands-On (Muppala) Android Overview 22

Page 23: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Example  Android  Manifest  File  <?xml  version="1.0"  encoding="u/-­‐8"?>  <manifest  xmlns:android="h5p://schemas.android.com/apk/res/android"              package="hkust.comp355.courseinfo”  android:versionCode="1”  android:versionName="1.0">          <applica5on  android:icon="@drawable/icon"  android:label="@string/app_name"          android:debuggable="true">                  <ac5vity  android:name=".welcome"                                      android:label="@string/app_name">                          <intent-­‐filter>                                  <ac5on  android:name="android.intent.acHon.MAIN"  />                                  <category  android:name="android.intent.category.LAUNCHER"  />                          </intent-­‐filter>                  </ac5vity>                  <ac5vity  android:name=".menu"                                      android:label="@string/app_name">                  </ac5vity>  </applica5on>          <uses-­‐sdk  android:minSdkVersion="8"  />  </manifest>    

AAD Hands-On (Muppala) Android Overview 23

Page 24: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Service

•  A  service  runs  in  the  background  for  an  indefinite  period  5me.  –  Example:  play  music,  network  download,  etc  –  Does  not  have  a  user  interface  –  extends  the  Service  base  class.  

•  We  can  bind  to  a  running  service  (or  start  the  service  if  it's  not  already  running)  

AAD Hands-On (Muppala) Android Overview 24

Page 25: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Broadcast  Receivers

•  Receives  and  reacts  to  broadcast  announcements  (Intents)  –  Many  broadcasts  originate  in  system  code.  –  E.g.  announcements  that  a  picture  is  taken,  that  the  5me  zone  has  

changed,  that  the  ba6ery  is  low,  etc.  –  Applica5ons  can  also  ini5ate  broadcasts.

•  E.g.  to  let  other  applica5ons  know  that  some  data  has  been  downloaded  to  the  device  and  is  available  for  them  to  use  

–  Do  not  have  a  user  interface  –  May  start  an  ac5vity  in  response  to  the  received  broadcast  –  May  also  use  no5fica5ons  to  alert  the  user  

AAD Hands-On (Muppala) Android Overview 25

Page 26: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Content  Providers

•  Makes  a  specific  set  of  the  applica5on's  data  available  to  other  applica5ons  –  Data  can  be  stored  in  the  file  system,  in  an  SQLite,  or  in  any  other  

manner  that  makes  sense  –  extends  the  ContentProvider  base  class  to  implement  a  standard  

set  of  methods  that  enable  other  applica5ons  to  retrieve  and  store  data  of  the  type  it  controls  

•  Applica5ons  do  not  call  these  methods  directly.  –  They  use  a  ContentResolver  object  and  call  its  methods  instead  –  A  ContentResolver  can  talk  to  any  content  provider  

AAD Hands-On (Muppala) Android Overview 26

Page 27: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Exercise:  Crea5ng  a  Simple  Applica5on  

Page 28: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  Create  a  new  Android  Project.  In  Eclipse,  click  File-­‐>  New  -­‐>  Android  project.    Meaning  of  different  fields:    –  Project  Name    

•  Use  MyInfo.  

–  Build  Target  •  Android  4.0.3.  

–  ApplicaHon  Name    •  Use  My  Personal  Info.  

–  Package  Name    •  Here  we  use  the  "hkust.cse.myinfo"  namespace.  

AAD Hands-On (Muppala) Android Overview 28

Page 29: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

–  Create  AcHvity    •  This  is  the  name  for  the  class  stub  that  will  be  generated  by  the  plugin.  This  

will  be  a  subclass  of  Android's  Ac5vity  class.  An  Ac5vity  is  simply  a  class  that  can  run  and  do  work.  It  can  create  a  UI  if  it  chooses,  but  it  doesn't  need  to.  As  the  checkbox  suggests,  this  is  op5onal,  but  an  Ac5vity  is  almost  always  used  as  the  basis  for  an  applica5on.  Here  we  use  MyInfoAc'vity.    

•  Now  that  your  Android  project  is  ready,  we  will  define  colors  for  the  applica5on  –  Find  the  folder  /res/Values,  right  click,  select  New-­‐>  Android  XML  File,  set  

the  file  name  as  colors.xml,  and  set  the  content  to  the  following  code:  <?xml  version="1.0"  encoding="u/-­‐8"?>  <resources>  <color  name=”myblue">#003366</color>  <color  name=”myyellow">#996600</color>  </resources>  

–  This  is  how  we  define  new  colors  that  can  be  used  in  our  applica5ons.    

AAD Hands-On (Muppala) Android Overview 29

Page 30: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  Open  strings.xml,  and  change  its  content  with  following  code:  <?xml  version="1.0"  encoding="u/-­‐8"?>  <resources>          <string  name="app_name”>My  Personal  Info</string>          <string  name="Welcome">Welcome  to</string>        <string  name=”MyName”>Your  Name\’s\nApp</string>  </resources>  

–  We  are  basically  defining  three  new  strings  that  we  can  use  for  the  text  in  the  user  interface  that  we  design  next.  It  is  very  easy  to  replace  these  string  values  with  other  values  to  reconfigure  the  applica5on  user  interface  without  needing  to  change  any  code.    

AAD Hands-On (Muppala) Android Overview 30

Page 31: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  Import  some  pictures  to  use  in  designing  the  user  interface:  There  are  icons  for  three  different  resolu5ons  (high,  medium  and  low):  hdpi,  mdpi  and  ldpi  –  On  the  course  page  you  will  find  four  Zip  files:  ldpi.zip,  mdpi.zip,  

hdpi.zip  and  xhdpi.zip.  Download  the  four  zip  files  to  the  respec5ve  folders  in  the  project  home  directory  (must  be  D:\workspace\MyInfo\res\drawable-­‐<type>)  and  unzip  them  

–  Then,  right  click  on  the  project  name  in  Eclipse,  and  select  refresh.  You  can  see  the  icons  appear  in  the  project  folder.    

–  The  Android  device  will  use  the  appropriate  icons  that  are  suitable  for  the  resolu5on  of  the  device.    

AAD Hands-On (Muppala) Android Overview 31

Page 32: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  Configure  the  user  interface  layout:  In  the  /res/layout  folder,  rename  main.xml  to  myinfo.xml  (just  press  F2  to  rename),  and  change  its  content  to  the  following  code:  

<?xml  version="1.0"  encoding="u`-­‐8"?>  <LinearLayout  xmlns:android="h6p://schemas.android.com/apk/res/android"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:background="@color/myblue"          android:orienta5on="ver5cal"  >      

AAD Hands-On (Muppala) Android Overview 32

Page 33: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

                 <TextView                  android:layout_width="match_parent"                  android:layout_height="wrap_content"                  android:layout_margin="20dp"                  android:gravity="center_ver5cal|center_horizontal"                  android:text="@string/Welcome"                  android:textColor="@android:color/white"                  android:textSize="14pt"  />                  

AAD Hands-On (Muppala) Android Overview 33

Page 34: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on                  <ImageView                  android:id="@+id/imageView1"                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:layout_gravity="center"                  android:layout_margin="40dp"                  android:src="@drawable/boy"  />            <TextView                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  android:gravity="center_ver5cal|center_horizontal"                  android:text="@string/MyName"                  android:textColor="@color/myyellow"                  android:textSize="14pt"  />            </LinearLayout>  

AAD Hands-On (Muppala) Android Overview 34

Page 35: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  What  we  have  done  by  modifying  the  myinfo.xml  file  is  to  define  the  new  user  interface  

•  We  use  the  TextView  layout  and  set  the  text  of  the  textview  layout  to  the  appropriate  string  value  –  Note  how  the  string  values  are  referenced  

•  We  use  ImageView  to  include  the  pictures.    

AAD Hands-On (Muppala) Android Overview 35

Page 36: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

A  Simple  Applica5on  

•  Then  change  the  content  of  cseinfo.java  file  and  replace  the  onCreate()  method  with  following  code:    

 public  void  onCreate(Bundle  savedInstanceState)  {                  super.onCreate(savedInstanceState);                  setContentView(R.layout.myinfo);          }  

•  Now  run  the  applica5on  and  no5ce  the  screen  layout  

AAD Hands-On (Muppala) Android Overview 36

Page 37: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Intents  

Page 38: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5va5ng  Components  

AAD Hands-On (Muppala) Android Overview 38

Ac5vity  

Service  

Broadcast  Receiver  

Intent  

Content  Provider  

Content  Resolver  

Page 39: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Intents

•  Intents  are  asynchronous  message  objects  to  ac5vate  components  other  than  content  providers  –  Message  consists  of  (for  ac5vi5es  and  services)  

•  Ac5on  being  requested  (MAIN/VIEW/EDIT/PICK/DELETE/DIAL/etc)  •  URI  of  the  data  to  act  upon  

–  For  broadcast  receivers  the  message  is  the  ac5on  being  announced  –  Example  

public void onClick(View v) { startActivity(new Intent(welcome.this,menu.class));}

AAD Hands-On (Muppala) Android Overview 39

Page 40: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Intents  (Contd.)  

•  Two  kinds  of  intents:  –  Explicit  intent:  explicitly  specify  the  component’s  name  that  should  

respond  to  the  intent  –  Implicit  intent:  Let  Android  locate  the  best  component  that  can  

respond  to  the  intent  •  Uses  intent  filters  for  this  (more  on  this  later)  •  Example  

startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://course.cse.ust.hk/comp4521/Description.html")));  

AAD Hands-On (Muppala) Android Overview 40

Page 41: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Shu�ng  Down  Components  

•  Ac5vi5es  and  services  need  to  be  shut  down  explicitly  •  No  need  to  explicitly  shut  down  content  provider  and  

broadcast  receiver  –  A  content  provider  ac5ve  only  while  responding  to  a  request  from  a  

ContentResolver  –  A  broadcast  receiver  ac5ve  only  while  responding  to  a  broadcast  

message  

•  Components  might  also  be  shut  down  by  the  system  –  When  they  are  no  longer  being  used  –  When  Android  must  reclaim  memory  for  more  ac5ve  components  

AAD Hands-On (Muppala) Android Overview 41

Page 42: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

The  Manifest  File  Revisited  

•  Applica5ons  declare  their  components  in  a  manifest  file  –  Bundled  into  the.apk  file  –  Before  Android  can  start  an  applica5on  component,  it  must  learn  that  

the  component  exists  

•  Structured  XML  file    –  Always  named  AndroidManifest.xml  for  all  applica5ons  –  Also  names  any  libraries  the  applica5on  needs  to  be  linked  against  

(besides  the  default  Android  library)    –  Iden5fies  any  permissions  the  applica5on  expects  to  be  granted  

AAD Hands-On (Muppala) Android Overview 42

Page 43: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Intent  Filters

•  A  component's  intent  filters  in  the  manifest  file  informs  Android  of  the  kinds  of  intents  the  component  is  able  to  handle. –  Example

<?xml  version="1.0"  encoding="u/-­‐8"?>  <manifest  xmlns:android="h5p://schemas.android.com/apk/res/android"              package="hkust.comp355.courseinfo”  android:versionCode="1”  android:versionName="1.0">          <applica5on  android:icon="@drawable/icon"  android:label="@string/app_name"          android:debuggable="true">                  <ac5vity  android:name=".welcome"                                      android:label="@string/app_name">                          <intent-­‐filter>                                  <ac5on  android:name="android.intent.acHon.MAIN"  />                                  <category  android:name="android.intent.category.LAUNCHER"  />                          </intent-­‐filter>                  </ac5vity>  .  .  .  

AAD Hands-On (Muppala) Android Overview 43

Page 44: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Intent  Filters  (Contd.)

•  A  component  can  have  any  number  of  intent  filters,  each  one  declaring  a  different  set  of  capabili5es.  

•  One  filter  may  indicate  that  the  ac5vity  is  the  entry  point  for  the  applica5on.  

•  Another  filter  may  declare  an  ac5on  that  the  ac5vity  can  perform  on  par5cular  type  of  data  

•  If  it  doesn't  have  any  filters,  it  can  be  ac5vated  only  by  intents  that  explicitly  name  the  component  as  the  target

AAD Hands-On (Muppala) Android Overview 44

Page 45: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Exercise:  Intents  and  Ac5vi5es  

Page 46: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Using  Implicit  Intents  

•  Open  the  cseinfo.xml  file  and  add  a  bu6on  widget:  <Bu6on  android:id="@+id/bu6on1"                                            android:layout_height="wrap_content"                                            android:layout_width="wrap_content”                                            android:text=“My  Web  Page”  />    

AAD Hands-On (Muppala) Android Overview 46

Page 47: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Using  Implicit  Intents  

•  Open  myinfo.java  to  add  an  onClick()  listener  to  the  bu6on  as  follows:  

Bu6on  bu6on=  (Bu6on)  findViewById(R.id.bu5on1);                      bu6on.setOnClickListener(new  Bu6on.OnClickListener()  {      public  void  onClick(View  v)  {            startAc5vity(new  Intent(Intent.ACTION_VIEW,                                              Uri.parse("h[p://www.cse.ust.hk/~muppala/")));          }    });  

AAD Hands-On (Muppala) Android Overview 47

Page 48: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Applica5on  Components  and  

Component  Lifecycle  

Page 49: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Applica5ons  and  Components •  An  Android  applicaHon  typically  

consists  of  one  or  more  related,  loosely  bound  ac5vi5es  for  the  user  to  interact  with,  typically  bundled  up  in  a  single  file  (with  an  .apk  suffix)  –  One  ac5vity  designated  as  the  

main  ac5vity  •  An  Android  applica5on  is  

composed  of  several  components  –  Every  applica5on  runs  in  its  own  

Linux  process  –  Process  created  whenever  any  one  

of  the  components  get  ac5vated  –  Every  component  has  a  managed  

lifecycle  

Applica5on  

Ac5vity  1   Ac5vity  2  

Service  1  

Broadcast  Receiver  

Service  2  

Content  Provider  

Ac5vity  3  

AAD Hands-On (Muppala) Android Overview 49

Page 50: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Component  Lifecycle  

•  Component  lifecycle  –  From  the  beginning  when  Android  instan5ates  them  to  respond  to  

intents  through  to  an  end  when  the  instances  are  destroyed  –  In  between,  they  may  some5mes  be  ac5ve  or  inac5ve  –  Ac5vi5es  may  be  visible  to  the  user  or  invisible  

AAD Hands-On (Muppala) Android Overview 50

Page 51: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Component  Lifecycles

•  Ac5vity  Lifecycle  –  Three  states  

•  Running,  paused  and  stopped  

–  If  an  ac5vity  is  paused  or  stopped,  the  system  can  drop  it  from  memory  either  by:  

–  asking  it  to  finish  (calling  its  finish()  method)  

–  simply  killing  its  process.    

• Ac5vity  in  the  foreground  of  the  screen  (at  the  top  of  the  ac5vity  stack  for  the  current  task)  

Resumed  

• Ac5vity  has  lost  focus  but  is  s5ll  visible  to  the  user  Paused  

• Ac5vity  is  completely  obscured  by  another  ac5vity  

• It  s5ll  retains  all  state  and  member  informa5on  

Stopped  

AAD Hands-On (Muppala) Android Overview 51

Page 52: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  Lifecycle  

•  As  an  ac5vity  transi5ons  from  state  to  state,  it  is  no5fied  of  the  change  by  calls  to  the  following  protected  methods:  void onCreate(Bundle savedInstanceState) void onStart() void onRestart() void onResume() void onPause() void onStop() void onDestroy()

AAD Hands-On (Muppala) Android Overview 52

Page 53: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  Lifecycle •  Three  nested  loops  for  the  en5re  

lifecycle  –  En're  life'me  

•  From  onCreate()  to  onDestroy()  •  An  ac5vity  does  all  its  ini5al  setup  of  

"global"  state  in  onCreate()  •  releases  all  remaining  resources  in  

onDestroy()  •  For  example,  create  that  thread  in  

onCreate()  and  then  stop  the  thread  in  onDestroy().  

–  Visible  Life5me  •  User  can  see  the  ac5vity  on-­‐screen,  

though  it  may  not  be  in  the  foreground  and  interac5ng  with  the  user.  

•  onStart()  and  onStop()  can  be  called  mul5ple  5mes,  as  the  ac5vity  alternates  between  being  visible  and  hidden  to  the  user.  

–  Foreground  Life5me  •  Ac5vity  is  in  front  of  all  other  ac5vi5es  

on  screen  and  is  interac5ng  with  the  user.  

En5re  Life5m

e  

Visib

le  Life

5me  

Foregrou

nd  

Life5m

e  

AAD Hands-On (Muppala) Android Overview 53

Page 54: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  Lifecycle

•  When  are  the  methods  called  in  an  ac5vity's  overall  lifecycle?  –  onCreate()  

•  Called  when  the  ac5vity  is  first  created  or  when  the  ac5vity  was  killed    –  onStart()  

•  Called  just  before  the  ac5vity  becomes  visible  to  user  

–  onRestart()  •  Called  a=er  the  ac5vity  has  been  stopped,  just  prior  to  it  being  started  again  

–  onResume()  •  Called  just  before  the  ac5vity  starts  interac5ng  with  the  user  •  At  this  point,  the  ac5vity  is  at  the  top  of  the  ac5vity  stack,  with  user  input  going  to  it  

AAD Hands-On (Muppala) Android Overview 54

Page 55: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  Lifecycle

•  When  are  the  methods  called  in  an  ac5vity's  overall  lifecycle?  –  onPause()  

•  Called  when  the  system  is  about  to  start  resuming  another  ac5vity  •  This  method  is  typically  used  to  commit  unsaved  changes  to  persistent  data,  stop  anima5ons  and  other  things  that  may  be  consuming  CPU,  and  so  on  

–  onStop()  •  Called  when  the  ac5vity  is  no  longer  visible  to  the  user  •  This  may  happen  because  it  is  being  destroyed,  or  because  another  ac5vity  has  been  resumed  and  is  covering  it  

–  onDestroy()  •  Called  before  the  ac5vity  is  destroyed  

AAD Hands-On (Muppala) Android Overview 55

Page 56: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Ac5vity  Lifecycle  

AAD Hands-On (Muppala) Android Overview 56

Page 57: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Saving  Ac5vity  State  

•  When  system  shuts  down  an  ac5vity,  user  may  expect  to  return  to  the  ac5vity  in  its  previous  state  

•  Make  use  of  two  methods  implemented  within  the  ac5vity  to  save  and  restore  state:  –  onSaveInstancestate():  Android  calls  this  before  making  the  ac5vity  

vulnerable  to  being  destroyed  (i.e.  before  onPause())  •  Save  state  in  a  Bundle  object  

–  onRestoreInstanceState():  Bundle  passed  both  to  onCreate()  and  the  onRestoreInstanceState()  which  is  called  just  before  onStart()  to  recreate  the  captured  state  

–  Both  methods  are  not  part  of  lifecycle,  as  they  need  not  be  called  when  the  user  inten5onally  destroys  the  ac5vity  

AAD Hands-On (Muppala) Android Overview 57

Page 58: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Saving  Ac5vity  State  

AAD Hands-On (Muppala) Android Overview 58

Page 59: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Coordina5ng  Ac5vi5es  

•  When  one  ac5vity  starts  another,  they  both  experience  lifecycle  transi5ons  –  One  pauses  and  may  stop,  while  the  other  starts  up.  –  On  occasion,  you  may  need  to  coordinate  these  ac5vi5es,  one  with  

the  other.  

•  The  order  of  lifecycle  callbacks  is  well  defined,  par5cularly  when  the  two  ac5vi5es  are  in  the  same  process.  For  example  if  Ac5vity  A  starts  Ac5vity  B:  1.  A's  onPause()  method  is  called.  2.  B's  onCreate(),  onStart(),  and  onResume()  methods  are  called  in  

sequence.  3.  Then,  if  A  is  no  longer  visible  on  screen,  its  onStop()  method  is  called.  

AAD Hands-On (Muppala) Android Overview 59

Page 60: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Service  Lifecycle

•  A  service  can  be  used  in  two  ways:  –  The  service  can  be  started  and  allowed  to  run  un5l  someone  stops  it  

or  stops  itself.  •  Started  by  calling  Context.startService()  •  Stopped  by  calling  Context.stopService()  

–  The  service  can  be  operated  programma5cally  using  an  interface  that  it  defines  and  exports  

•  Clients  establish  a  connec5on  to  the  Service  object  and  use  that  connec5on  to  call  into  the  service.  

•  Connec5on  established  by  calling  Context.bindService()  •  Connec5on  closed  by  calling  Context.unbindService()  

AAD Hands-On (Muppala) Android Overview 60

Page 61: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Service  Lifecycle

•  Service  lifecycle  methods    –  They  are  public,  not  protected:  void  onCreate()    void  onStart(Intent  intent)    void  onDestroy()  

AAD Hands-On (Muppala) Android Overview 61

Page 62: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Broadcast  Receiver  Lifecycle

•  Single  callback  method  void  onReceive(Context  curContext,  Intent  broadcastMsg)  

•  When  a  broadcast  message  arrives  for  the  receiver,  Android  calls  the  method  and  passes  it  the  Intent  object  containing  the  message  

–  Ac5ve  only  when  the  onReceive()  is  being  executed.  Becomes  inac5ve  on  exit  of  this  method  

–  A  process  with  an  ac5ve  broadcast  receiver  is  protected  from  being  killed  but  a  process  with  only  inac5ve  components  can  be  killed  by  the  system  at  any  5me.  

AAD Hands-On (Muppala) Android Overview 62

Page 63: Android’Overview - HKUSTmuppala/aad/notes/Android_Overview.pdf · AAD Hands-On (Muppala) Android Overview 5 Name* Version* API*Level* Release*Date* LinuxKernel Base’ 1.0 1 Sep.2008’

Summary  

•  Android  Architecture:  Layered;  –  Applica5ons,  framework,  libraries  and  run5me,  and  kernel  

•  Android  applica5on  components  –  Ac5vity,  service,  content  provider  and  broadcast  receiver  

•  Intents  •  Component  Lifecycle  

–  Lifecycle  methods  

AAD Hands-On (Muppala) Android Overview 63