android service patterns
TRANSCRIPT
![Page 1: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/1.jpg)
Android Service Patterns AIDL Services Aren’t That Hard…
Shree Kumar InnoMinds Software
![Page 2: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/2.jpg)
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
![Page 3: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/3.jpg)
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 patterns
Android Service Patterns | DroidCon India 2011
![Page 4: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/4.jpg)
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, IPC
Android Service Patterns | DroidCon India 2011
![Page 5: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/5.jpg)
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
![Page 6: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/6.jpg)
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
![Page 7: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/7.jpg)
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 interface Android Service Patterns | DroidCon India 2011
![Page 8: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/8.jpg)
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 versioning
Android Service Patterns | DroidCon India 2011
![Page 9: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/9.jpg)
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 type
Android Service Patterns | DroidCon India 2011
![Page 10: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/10.jpg)
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 => starvation
Android Service Patterns | DroidCon India 2011
![Page 11: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/11.jpg)
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 DeadObjectException Android Service Patterns | DroidCon India 2011
![Page 12: Android Service Patterns](https://reader035.vdocument.in/reader035/viewer/2022062319/555116f7b4c905b1138b4cb4/html5/thumbnails/12.jpg)
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 Tutorial
Android Service Patterns | DroidCon India 2011