Android Service Patterns  AIDL Services Aren’t That Hard…          Shree Kumar          InnoMinds Software
Motivation • Writing services is useful & fun       – Not to mention, painful ! • Documentation is present, but…       – S...
Objectives • Clarification on the term “Patterns”       – NOT design patterns a-la Grady Booch! • This talk gives you     ...
Intro to Android Services • The “Service” concept       – Application component       – Typically used for long-running op...
AIDL based IPC Services • Android Interface Definition Language • Inter-Process Communication to       – Expose interfaces...
Why all the pain? • “natural” interactions with the service       – Calls & callbacks       – With complex objects as para...
Hello, AIDL Service! • Implements onBind()       – Exposes interface by implementing stubs • Exports via AndroidManifest.x...
What’s not-so-nice there ? • No control over binding       – onBind() return value is cached! • No control over threading ...
Common Service Patterns • Exclusive access to shared resource       – The “lock” pattern • Simultaneous access to shared r...
The “lock” pattern • Exclusive access using “locks”       – Take lock before use       – Release lock after use • Looks si...
Locked Service Implementation • Associate locks with calling process • Worker thread for serialization       – Started on ...
Shared Access Service • “broadcast” method       – Shares a state update with multiple listeners       – Broadcast receive...
Questions ?   Shree Kumar   shree.shree@gmail.comhttp:://www.shreekumar.in/
Upcoming SlideShare
Loading in...5
×

Android Service Patterns

3,365

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,365
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
57
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Android Service Patterns

  1. 1. Android Service Patterns AIDL Services Aren’t That Hard… Shree Kumar InnoMinds Software
  2. 2. Motivation • Writing services is useful & fun – Not to mention, painful ! • Documentation is present, but… – Scattered – Deals mostly with mechanics • Can a 45 minute session make a difference ?Android Service Patterns | DroidCon India 2011
  3. 3. Objectives • Clarification on the term “Patterns” – NOT design patterns a-la Grady Booch! • This talk gives you – Quick overview of AIDL based Services – Features & Pains – Writing useful services as patternsAndroid Service Patterns | DroidCon India 2011
  4. 4. Intro to Android Services • The “Service” concept – Application component – Typically used for long-running operations – Does not automatically create • new processes or threads • Can be roughly classified based on – Interaction Mechanism : Started, Bound – Application Boundaries : In-process, IPCAndroid Service Patterns | DroidCon India 2011
  5. 5. AIDL based IPC Services • Android Interface Definition Language • Inter-Process Communication to – Expose interfaces to other applications • More complicated than “Messenger”Android Service Patterns | DroidCon India 2011
  6. 6. Why all the pain? • “natural” interactions with the service – Calls & callbacks – With complex objects as parameters • And all these across process boundaries!Android Service Patterns | DroidCon India 2011
  7. 7. Hello, AIDL Service! • Implements onBind() – Exposes interface by implementing stubs • Exports via AndroidManifest.xml • Client apps bind via Intents – Use interface methods – Callbacks require app to implement stubs corresponding to callback interfaceAndroid Service Patterns | DroidCon India 2011
  8. 8. What’s not-so-nice there ? • No control over binding – onBind() return value is cached! • No control over threading • Code messier than in-process interface – No support for exceptions – Dead remote object exceptions => messy code – Callbacks not invoked in UI context • No concept of versioningAndroid Service Patterns | DroidCon India 2011
  9. 9. Common Service Patterns • Exclusive access to shared resource – The “lock” pattern • Simultaneous access to shared resource – “serialize”, “broadcast” patterns • Less common – Service versioning – Multiple objects of the same typeAndroid Service Patterns | DroidCon India 2011
  10. 10. The “lock” pattern • Exclusive access using “locks” – Take lock before use – Release lock after use • Looks simple ? – Multi-app access – Appropriate threading model – Improper app lock handling => starvationAndroid Service Patterns | DroidCon India 2011
  11. 11. Locked Service Implementation • Associate locks with calling process • Worker thread for serialization – Started on service creation – Producer-consumer modeling • Java.util.concurrent provides ample support • Partial solution for starvation – Evict clients on DeadObjectExceptionAndroid Service Patterns | DroidCon India 2011
  12. 12. Shared Access Service • “broadcast” method – Shares a state update with multiple listeners – Broadcast receivers don’t make the cut • Can’t bind a Service inside them • Implementation – Reuse “listener” concept • Clients add/remove listeners – Service maintains list of listeners • Updates listeners when needed • Removes dead objects – Reference : MindTheRobot Android Architecture TutorialAndroid Service Patterns | DroidCon India 2011
  13. 13. Questions ? Shree Kumar shree.shree@gmail.comhttp:://www.shreekumar.in/
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×