13

Under what circumstances would using AIDL to define a service interface be the correct decision (rather than just creating an extension to the service class)?

Janusz
  • 187,060
  • 113
  • 301
  • 369
MalcomTucker
  • 7,407
  • 14
  • 72
  • 93

4 Answers4

8

You need to use AIDL if you want a class outside of your application's process to access the Service. If you're only using the service from inside your application, you can use a local service.

Erich Douglass
  • 51,744
  • 11
  • 75
  • 60
7

Merely extending a service class will not allow your service to expose its methods to outside entities. If you want your service to be exposed/used by code which runs out of your android app, you will have to define an AIDL for it. This AIDL will be shared and be formed as contract for your service. Refer this http://developer.android.com/guide/components/aidl.html.

Dave
  • 141
  • 2
  • 14
Tushar Tarkas
  • 1,592
  • 13
  • 25
  • 3
    But assuming only your app makes use of the service, there is no reason to use aidl - is that correct? – MalcomTucker Jul 26 '10 at 13:49
  • 1
    Correct. But then why would you like to create a service in that case either. – Tushar Tarkas Jul 26 '10 at 13:53
  • 1
    I created a local service in my app to do a bigger download. In that way I could have some more control and security then just an Async Task that can be killed all every time. As I understand a service that is bound to an activity is not that likely to be killed if resources are scarce. – Janusz Jul 26 '10 at 14:11
1

1.when to use aidl based service.

A few benefits can be gained by segment part of your code into backend service:

  • decouple front-end and back-end
  • memory/cpu intensive processing can be stacked to backend service, GC in service will not affect front-end user experience
  • service crash will not bring down the whole APP

2.how to create such a service

I have written a good library, you can refer as an example http://github.com/zhchang/hogwarts

zhao chang
  • 211
  • 3
  • 8
0

AIDL

The Android Interface Definition Language (AIDL) allows developers to define a programming interface that the client and server use to communicate with each other using Inter-Process Communication (IPC).

This article shows how to connect to a running service in Android, and how to retrieve the data from the remote/running service.

Example of IPC mechanism

Let RemoteService be a client service and RemoteServiceClient be an Activity to communicate with the remote service.

One service provides information about the mathematic operations like addition, subtraction, and multiplication for the given two integers. To expose the functionality of what Service can do, create an .aidl file in the project directory.

AIDL Example

Ramkailash
  • 1,852
  • 1
  • 23
  • 19