Lecture 7: Android Services By: Eliav Menachi
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
Local Services Started via Context.startService() Runs until: Context.stopService() stopSelf() See SimpleService example
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
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
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
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
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
3. Implementing AIDL Interface Write a class that extends android.app.Service Implement: Implement the interface method Implement the onBind() method
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>
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()
Service lifecycle