Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Android Services

Similar presentations


Presentation on theme: "Lecture 7: Android Services"— Presentation transcript:

1 Lecture 7: Android Services
By: Eliav Menachi

2 Services Services are components that run in the background, without a user interface Android supports two types of services:. Local service: not accessible from other applications running on the device Remote service: accessible from other applications in addition to the application hosting the service

3 Local Services Started via Context.startService() Runs until:
Context.stopService() stopSelf() See SimpleService example

4 Remote service Service that can be consumed by other processes via Remote Procedure Call (RPC) Android Interface Definition Language (AIDL): define the interface that will be exposed to clients

5 Building Remote service
To build a remote service, you do the following: Write an AIDL file that defines your interface to clients Add the AIDL file to your Eclipse project - generate a Java interface from the AIDL file Implement the service and return the interface from the onBind() method Add the service configuration to your AndroidManifest.xml

6 1. Implementing AIDL AIDL is a simple syntax that lets you declare an interface with one or more methods The parameters and return values can be of any type, even other AIDL-generated interfaces you must import all non-built-in types, even if they are defined in the same package as your interface

7 AIDL Data types Data types that AIDL can support:
Primitive types (int, boolean…) String List - the other side will receive an ArrayList Map - the other side will receive a HashMap CharSequence Other AIDL-generated interfaces Custom classes that implement the Parcelable protocol

8 2. Generate a Java interface from AIDL
Add the AIDL file to your Eclipse project Eclipse will automatically call the AIDL compiler and will generate the Java interface from the AIDL file

9 3. Implementing AIDL Interface
Write a class that extends android.app.Service Implement: Implement the interface method Implement the onBind() method

10 4. Service Configuration
Add the service configuration to your AndroidManifest.xml <service android:name=".AIDLServiceImpl"> <intent-filter> <action android:name="bgu.eliav.AIDLServiceInterface" /> </intent-filter> </service>

11 Calling the Service copy the AIDL file to the client project
AIDL compiler creates the interface Extends the ServiceConnection get a reference to the service call the bindService()

12 Service lifecycle


Download ppt "Lecture 7: Android Services"

Similar presentations


Ads by Google