Upload
vladimir-kulyukin
View
1.325
Download
2
Tags:
Embed Size (px)
Citation preview
Android MobAppDev
Bound Services
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline
● Android Binder● Binder Terminology● AIDL● Sample Binder Service
Android Binder
Inter-Process Communication (IPC)
● Binder is an Inter-Process Communication (IPC) framework for developing OOP services for the Android OS
● IPC is a framework for sharing data among multiple processes
● It is an OOP framework for OS kernel development
● It is used by many native Android applications and services
Inter-Process Communication (IPC)
● IPC is used for Remote Procedure Calls (RPC), data sharing, memory sharing
● IPC encourages modularity, security, stability● Each process on Android has a unique ID and runs
in its sandboxed space
Binder Usage
● Many Android applications and services use and depend on Binder
● ActivityManagerService does life-cycle callbacks (e.g., onDestroy()) via Binder
● Android OS depends on the Binder framework
IPC Mechanisms● Files● Sockets● Semaphores● Shared Memory● Intents● Content Providers● Android Binder
Binder Advantages
● Binder has a built-in reference counting mechanism
● Binder has a service termination mechanism● When a Binder service does not have any
client references, the service's owner is notified
Binder Advantages
● Android has a native binder interface implemented in libbinder
● Android Open Source project has a Java IBinder interface and a wrapper around libbinder
● There is no POSIX standard for Binder
Binder Terms
● Driver – driver that supports communication among processes
● Protocol – communication protocol for Binder Driver
● Interface – interface that each Binder object must implement
● Object – an object that implements some Binder Interface
Binder Terms
● Token – unique 32-bit integer ID for a Binder object
● Service – implementation of a specific Binder object
● Client – a user of some Binder Service● Transaction – an operation invoked on a
remote Binder object on the Binder protocol
Binder Terms● Parcel – a unit of data● Marshalling – process of conversion of data
structures into parcels● Unmarshalling – process of conversion of parcels
into data structures● Proxy – an implementation of some AIDL
interface that marshals and unmarshals data on the client
● Stub – an implementation of some AIDL interface that marshals and unmarshals data on the server
Binder Discovery● Clients get handles to services through the
ServiceManager● Services must be registered with
ServiceManager● Unregistered services cannot be discovered● List of running services can be obtained with
adb shell service list
AIDL
AIDL
● AIDL stands for Android Interface Definition Language
● AIDL is the language for Binder interfaces● Each service interface is defined a .aidl file● Android SDK has the aidl build tool that
compiles .aidl files and places the generated code in the gen/ directory
Some Supported Data Types● boolean, byte, char, int, long, float● boolean[], byte[], char[], int[], long[], float[]● CharSequence● String● Serializable● Map● android.os.Bundle● List● android.os.Parcelable
Parameters
● AIDL methods can return void or a value● Non-primitive parameters must have directional
tags: in, out, inout● Static files are not supported ● The following exceptions are supported:
NullException, IllegalStateException, IllegalArgumentException, BadParcelableException, SecurityException
Sample Binder Service
Problem
Develop an Binder service that computes the sums of the first n Catalan and Fibonacci numbers.
source code is in BoundSumService repo
Development Steps● Develop a common library, BindSumLib, that will
be used by the service (BindSumService) and the client (BindSumClient)
● Develop the service (BindSumService) and include BindSumLib as a library
● Install the service on Android● Develop the client (BindSumClient) and include
BindSumLib as a library● Install the client on Android
BindSumLib
Interfacepackage org.vkedco.mobappdev.bindsumlib;
import org.vkedco.mobappdev.bindsumlib.SumRequest;
import org.vkedco.mobappdev.bindsumlib.SumResponse;
interface IBindSumService
{
long fibonacciSum(in long n); // sum of the first n fibonacci numbers
long catalanSum(in long n); // sum of the first n catalan numbers
SumResponse sum(in SumRequest req);
}
source code is in IBindSumService.aidl
SumRequest.aidl
package org.vkedco.mobappdev.bindsumlib;
parcelable SumRequest;
SumResponse.aidl
package org.vkedco.mobappdev.bindsumlib;
parcelable SumRequest;
package org.vkedco.mobappdev.bindsumlib;
parcelable SumResponse;
AndroidManifest.xml
package org.vkedco.mobappdev.bindsumlib;
parcelable SumRequest;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.vkedco.mobappdev.bindsumlib"
android:versionCode="1"
android:versionName="1.0" >
</manifest>
BindSumService
Stub & Service● IBindSumServiceImpl extends
IBinderService.Stub and implements all methods in IBindSumService.aidl
● BindSumService extends Service and implements all Service callbacks: onCreate(), onBind(), onUnbind(), onDestroy()
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.vkedco.mobappdev.bindsumservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<service android:name=".BindSumService">
<intent-filter>
<action android:name="org.vkedco.mobappdev.bindsumlib.IBindSumService" />
</intent-filter>
</service>
</application>
</manifest>
BindSumClient
GUI