
Android Services & Local IPC: The Activator Pattern (Part 2) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA Android Services & Local IPC Douglas C. Schmidt Learning Objectives in this Part of the Module • Understand how the Activator pattern is applied in Android activate resource startService() Process.start() use resource use (re)assemble resource resource onStartCommand() 2 Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers public abstract class Service • Encapsulate each distinct unit of extends ContextWrapper app functionality into a self- implements ComponentCallbacks2 contained service { public abstract IBinder onBind(Intent intent); ... } frameworks/base/core/java/android/app/Service.java3 has the source code Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation Intent Purpose • Define services & service identifiers Element • Encapsulate each distinct unit of Name Optional name for a app functionality into a self- component contained service Action A string naming the • Examples of service identifier representations include URLs, action to perform or the IORs, TCP/IP port numbers & action that took place & host addresses, Android Intents, is being reported etc. Data URI of data to be acted on & the MIME type of that data Category String giving additional info about the action to execute frameworks/base/core/java/android/content/Intent.java4 has the source code Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers Android Purpose Service • Identify services to activate & deactivate on demand Media Provides “background” Playback audio playback • Determine overhead of activating Service capabilities & deactivating services on-demand vs. keeping them alive for the Exchange Send/receive email duration of the system vs. security Email messages to an Service implications, etc. Exchange server SMS & Manage messaging MMS operations, such as Services sending data, text, & PDU messages Alert Handle calendar event Service reminders packages/apps has source code for5 many Bound & Started Services Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define service execution context representation • e.g., an OS process/thread or middleware container frameworks/base/services/java/com/android/server/am/ActivityManagerService.java6 Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define service execution context representation • Define service registration strategy • e.g., static text file or dynamic object registration <service android:name= "com.android.music.MediaPlaybackService" android:exported="false"/> frameworks/base/services/java/com/android/server/am/ActivityManagerService.java7 Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define service execution context representation • Define service registration strategy • Define service initialization strategy • e.g., stateful vs. stateless services Android Services are responsible for8 managing their own persistent state Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers Started Service Bound Service • Identify services to activate & • Service runs • Multiple clients deactivate on demand indefinitely & can bind to must stop itself same Service • Develop service activation & by calling deactivation strategy • When all of stopSelf() them unbind, • Define service execution • A component can the system context representation also stop the destroys the • Define service registration strategy service by calling Service stopService() • The Service • Define service initialization strategy • When Service is does not need • Define service deactivation strategy stopped, Android to stop itself an • e.g., service-triggered, client- destroys it triggered, or activator-triggered deactivation developer.android.com/guide/components/9 services.html#Lifecycle has more Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define interoperation between services & service execution context • Typically implemented via some type of lifecycle callback hook methods developer.android.com/guide/components/10 services.html#LifecycleCallbacks Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define interoperation between services & service execution context • Implement the activator • Determine the association between activators & services • e.g., singleton (shared) vs. exclusive vs. distributed activator The Android Activity Manager Service11 is a singleton activator Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Implementation • Define services & service identifiers • Identify services to activate & deactivate on demand • Develop service activation & deactivation strategy • Define interoperation between services & service execution context • Implement the activator • Determine the association between activators & services • Determine the degree of transparency • e.g., explicit vs. transparent activator Android Started & Bound Services12 use an explicit activator model Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Applying Activator in Android • The NetworkSettings Activity uses the Activator pattern to launch the NetworkQueryService to assist in querying the network for service availability NetworkSettings loadNetworksList() onCreate() 1 startService() Call startService() to launch mCallback the NetworkQueryService & keep it running onQueryComplete() mNetworkQuery ServiceConnection ActivityManagerService onServiceConnected() startService() packages/apps/Phone/src/com/android/phone/NetworkSetting.java13 has source code Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Applying Activator in Android • The NetworkSettings Activity uses the Activator pattern to launch the NetworkQueryService to assist in querying the network for service availability NetworkSettings NetworkQueryService loadNetworksList() onCreate() 1 mCallback onQueryComplete() mNetworkQuery ServiceConnection ActivityManagerService onServiceConnected() Process.start() startService() 2 frameworks/base/services/java/com/android/server/am/ActivityManagerService.java14 Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Applying Activator in Android • The NetworkSettings Activity uses the Activator pattern to launch the NetworkQueryService to assist in querying the network for service availability NetworkSettings NetworkQueryService loadNetworksList() mBinder startNetworkQuery() onCreate() 1 mHandler mCallback handleMessage() onQueryComplete() onBind() mNetworkQuery ServiceConnection ActivityManagerService onServiceConnected() 3 initialize startService() 2 onCreate() Service packages/apps/Phone/src/com/android/phone/NetworkQueryService.java15 has source Android Services & Local IPC Douglas C. Schmidt Activator POSA4 Design Pattern Applying Activator in Android • The NetworkSettings Activity uses the Activator pattern to launch the NetworkQueryService to assist in querying the network for service availability NetworkSettings NetworkQueryService loadNetworksList() mBinder startNetworkQuery() 4 Call bindService() to onCreate() bindService() get a binder object for 1 mHandler use with async oneway mCallback method calls handleMessage() onQueryComplete() onBind() mNetworkQuery ServiceConnection ActivityManagerService onServiceConnected() Process.start() 3 startService() 2 onCreate() packages/apps/Phone/src/com/android/phone/NetworkSetting.java16 has source code Android Services & Local IPC Douglas C. Schmidt Summary activate resource startService() Process.start() use resource use (re)assemble resource resource onStartCommand() • The Android Started & Bound Services implement the Activator pattern 17 Android Services & Local IPC Douglas C. Schmidt Summary activate resource startService() Process.start() use resource use (re)assemble resource resource onStartCommand() • The Android Started & Bound Services implement the Activator pattern • These Services can process requests in background processes or threads • Processes can be configured depending on directives in the AndroidManifest.xml file 18 Android Services & Local IPC Douglas C. Schmidt Summary activate resource startService() Process.start() use resource use (re)assemble resource resource onStartCommand()
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages99 Page
-
File Size-