Slide 1

Slide 1 text

Android  Services     &   Background  Tasks         José  Juan  Sánchez  Hernández   @josejuansanchez  

Slide 2

Slide 2 text

Ac

Slide 3

Slide 3 text

Process   Applica

Slide 4

Slide 4 text

$  adb  shell  ps   USER PID PPID VSIZE RSS WCHAN PC NAME ... u0_a87 6229 1940 516552 31484 ffffffff 00000000 S com.android.calendar u0_a45 6247 1940 527692 31104 ffffffff 00000000 S com.google.android.calendar system 6276 1940 508328 27496 ffffffff 00000000 S com.sec.android.app.mt system 6292 1940 508644 27284 ffffffff 00000000 S com.sec.android.app.popupuir system 6306 1940 509388 28020 ffffffff 00000000 S com.sec.android.app.SecSetup system 6320 1940 515244 33816 ffffffff 00000000 S com.wssyncmldm u0_a54 6336 1940 510468 27460 ffffffff 00000000 S com.sec.android.app.controlp u0_a118 6350 1940 508704 28976 ffffffff 00000000 S com.sec.android.daemonapp.ap u0_a158 6362 1940 509020 29216 ffffffff 00000000 S com.sec.android.sCloudSync u0_a83 6375 1940 516300 36124 ffffffff 00000000 S com.osp.app.signin u0_a142 6393 1940 514340 32592 ffffffff 00000000 S com.google.android.keep u0_a10 6414 1940 508428 28252 ffffffff 00000000 S com.sec.android.provider.sme u0_a85 6427 1940 518356 30832 ffffffff 00000000 S com.android.browser u0_a50 6474 1940 533152 43816 ffffffff 00000000 S com.google.android.tts ... Zygote   4/50   $  adb  shell  ps  zygote   USER PID PPID VSIZE RSS WCHAN PC NAME root 1940 1 500544 22144 ffffffff 00000000 S zygote

Slide 5

Slide 5 text

The  manifest  entry  for  each  type  of  component  element:       , ,    and       supports  an  android:process  a_ribute  that  can  specify  a  process  in  which  that   component  should  run.  You  can  set  this  a_ribute  so  that  each  component  runs  in  its   own  process  or  so  that  some  components  share  a  process  while  others  do  not.       You  can  also  set  android:process  so  that  components  of  different  applica  element  also  supports  an  android:process  a_ribute,  to   set  a  default  value  that  applies  to  all  components.     Processes   5/50  

Slide 6

Slide 6 text

Process   Applica

Slide 7

Slide 7 text

Ac

Slide 8

Slide 8 text

UI   Thread   Background  Threads   8/50   UI   Thread   BG   Thread   BG   Thread   2.  Do  long     running  task   1   1   2.  Do  long     running  task   3.  Report  result  or  update  UI  

Slide 9

Slide 9 text

Android  Scheduling   9/50   Process  rank     1.  Foreground     2.  Visible     3.  Service     4.  Background     5.  Empty     Foreground  Thread  Group   App  A   Background  Thread  Group   App  A   >  90  %   <  10  %   CPU  execu

Slide 10

Slide 10 text

Android  Scheduling   10/50   Source:  Efficient  Android  Threading  /  Anders  Göransson   Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); Low  background  thread  priority  =  Be_er  responsiveness  

Slide 11

Slide 11 text

Asynchronous  Techniques   11/50   •  Thread   •  Executor   •  Handler  Thread   •  AsyncTask   •  IntentService   •  AsyncQueryHandler   •  Loader       Thread   Handler   Thread     Executor     Intent   Service   Async   Query   Handler   Loader   Async   Task   Source:  Efficient  Android  Threading  /  Anders  Göransson  

Slide 12

Slide 12 text

Lifecycles  and  Memory  Leaks   12/50   Time   Ac

Slide 13

Slide 13 text

Lifecycles  and  Memory  Leaks   13/50   Time   Ac

Slide 14

Slide 14 text

Lifecycles  and  Memory  Leaks   14/50   Time   Ac

Slide 15

Slide 15 text

15/50   Time   Lifecycles  and  Memory  Leaks   Source:  Efficient  Android  Threading  /  Anders  Göransson   Avoid  Memory  Leaks     •  Short  tasks.   •  Cancella

Slide 16

Slide 16 text

Applica

Slide 17

Slide 17 text

Applica

Slide 18

Slide 18 text

Services   18/50   What  is  a  Service?   •  Is  a  component  that  can  perform  long-­‐running  opera

Slide 19

Slide 19 text

Process   Applica

Slide 20

Slide 20 text

Applica

Slide 21

Slide 21 text

Ac

Slide 22

Slide 22 text

Example:  LocalService  /  1  Process   22/50  

Slide 23

Slide 23 text

Example:  LocalService  /  1  Process   23/50  

Slide 24

Slide 24 text

The  service  should  remain  stopped  un

Slide 25

Slide 25 text

Example:  LocalService  /  1  Process   25/50   START_NOT_STICKY If  the  system  kills  the  service,  do  not  recreate  the  service,  unless  there  are  pending  intents   to  deliver.  This  is  the  safest  op

Slide 26

Slide 26 text

Don’t  forget  it!!   26/50  

Slide 27

Slide 27 text

Good  Prac

Slide 28

Slide 28 text

Example:  LocalService  /  1  Process   28/50  

Slide 29

Slide 29 text

29/50  

Slide 30

Slide 30 text

Ac

Slide 31

Slide 31 text

Example:  RemoteService  /  2  Processes   31/50  

Slide 32

Slide 32 text

Example:  RemoteService  /  2  Processes   32/50  

Slide 33

Slide 33 text

This  technique  must  always  be  used  carefully  and     most  apps  should  not  run  mul#ple  processes   33/50   You  can  use  mul

Slide 34

Slide 34 text

Tip  ☺   How  to  debug  mul

Slide 35

Slide 35 text

35/50   Process  rank:  Foreground     $  adb  shell  ps  |  grep  "com.google.android.music"   USER                  PID              PPID    VSIZE          RSS          WCHAN        PC                    NAME   u0_a137      17837  1940    604896  64268  ffffffff  00000000  S  com.google.android.music:ui   u0_a137      17854  1940    553744  39768  ffffffff  00000000  S  com.google.android.music:main   Example:  Google  Play  Music   Process  rank:  Visible  

Slide 36

Slide 36 text

Ac

Slide 37

Slide 37 text

Don’t  forget  it!!   37/50   Example:  Service  +  HandlerThread  /  1  Process  

Slide 38

Slide 38 text

38/50   Example:  Service  +  HandlerThread  /  1  Process  

Slide 39

Slide 39 text

Example:  Service  +  HandlerThread  /  1  Process   39/50  

Slide 40

Slide 40 text

40/50  

Slide 41

Slide 41 text

Ac

Slide 42

Slide 42 text

Example.  IntentService  /  1  Process   42/50   You  never  have  to  call  stopSelf().     Stops  itself  when  it  runs  out  of  work.  

Slide 43

Slide 43 text

Example.  IntentService  /  1  Process   43/50  

Slide 44

Slide 44 text

1)  When  to  use?   •  The  Service  can  be  used  in  tasks  with  no  UI,  but  shouldn't  be  too  long.  If  you  need  to   perform  long  tasks,  you  must  use  threads  within  Service  or  use  another  process.   •  The  IntentService  can  be  used  in  long  tasks  usually  with  no  communica

Slide 45

Slide 45 text

There  are  several  ways  to  get  results  from  a  Service  back  to  an  invoking  Ac

Slide 46

Slide 46 text

46/50   Example:  IntentService  and  Broadcast  Receiver   1.  registerReceiver   mIntentReceiver   mIntentFilter   onReceive()   3.  onReceive     BroadcastReceiver   Ac

Slide 47

Slide 47 text

Process   Applica

Slide 48

Slide 48 text

Ac

Slide 49

Slide 49 text

Ac

Slide 50

Slide 50 text

Android  Services     &   Background  Tasks         José  Juan  Sánchez  Hernández   @josejuansanchez