mobappdev (fall 2013): android bound services

30
Android MobAppDev Bound Services Vladimir Kulyukin www.vkedco.blogspot.com

Upload: vladimir-kulyukin

Post on 11-May-2015

1.323 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: MobAppDev (Fall 2013): Android Bound Services

Android MobAppDev

Bound Services

Vladimir Kulyukin

www.vkedco.blogspot.com

Page 2: MobAppDev (Fall 2013): Android Bound Services

Outline

● Android Binder● Binder Terminology● AIDL● Sample Binder Service

Page 3: MobAppDev (Fall 2013): Android Bound Services

Android Binder

Page 4: MobAppDev (Fall 2013): Android Bound Services

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

Page 5: MobAppDev (Fall 2013): Android Bound 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

Page 6: MobAppDev (Fall 2013): Android Bound Services

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

Page 7: MobAppDev (Fall 2013): Android Bound Services

IPC Mechanisms● Files● Sockets● Semaphores● Shared Memory● Intents● Content Providers● Android Binder

Page 8: MobAppDev (Fall 2013): Android Bound Services

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

Page 9: MobAppDev (Fall 2013): Android Bound Services

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

Page 10: MobAppDev (Fall 2013): Android Bound Services

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

Page 11: MobAppDev (Fall 2013): Android Bound Services

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

Page 12: MobAppDev (Fall 2013): Android Bound Services

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

Page 13: MobAppDev (Fall 2013): Android Bound Services

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

Page 14: MobAppDev (Fall 2013): Android Bound Services

AIDL

Page 15: MobAppDev (Fall 2013): Android Bound Services

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

Page 16: MobAppDev (Fall 2013): Android Bound Services

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

Page 17: MobAppDev (Fall 2013): Android Bound Services

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

Page 18: MobAppDev (Fall 2013): Android Bound Services

Sample Binder Service

Page 19: MobAppDev (Fall 2013): Android Bound Services

Problem

Develop an Binder service that computes the sums of the first n Catalan and Fibonacci numbers.

source code is in BoundSumService repo

Page 20: MobAppDev (Fall 2013): Android Bound Services

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

Page 21: MobAppDev (Fall 2013): Android Bound Services

BindSumLib

Page 22: MobAppDev (Fall 2013): Android Bound Services

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

Page 23: MobAppDev (Fall 2013): Android Bound Services

SumRequest.aidl

package org.vkedco.mobappdev.bindsumlib;

parcelable SumRequest;

Page 24: MobAppDev (Fall 2013): Android Bound Services

SumResponse.aidl

package org.vkedco.mobappdev.bindsumlib;

parcelable SumRequest;

package org.vkedco.mobappdev.bindsumlib;

parcelable SumResponse;

Page 25: MobAppDev (Fall 2013): Android Bound Services

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>

Page 26: MobAppDev (Fall 2013): Android Bound Services

BindSumService

Page 27: MobAppDev (Fall 2013): Android Bound Services

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()

Page 28: MobAppDev (Fall 2013): Android Bound Services

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>

Page 29: MobAppDev (Fall 2013): Android Bound Services

BindSumClient

Page 30: MobAppDev (Fall 2013): Android Bound Services

GUI