Pages

Label

Rabu, 30 Januari 2019

AIDL and its uses in Android

Hello Guys !!! Hope You all are doing well. Today I am going to discuss AIDL and its uses in Android Application. 

AIDL (Android Interface Definition Language) is similar to other IDLs.
It allows us to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for us with AIDL.

# When to use AIDL

  1. Clients from different applications want to access your service for IPC.
  2. Your service want to handle multithreading for IPC(any method defined in service can be executed simultaneously by more than one application).
  3. If you want to share data and control something in another application.
  4. You want to create some new functionalities and distribute them as a library.


# AIDL Implementation Steps
In order to allow for one application to call into another, we typically have to:
  1. Define the AIDL interface
  2. Implement the Stub for the remote service
  3. Expose the remote service to the local client
# The data types supported by AIDL
  • All primitive types (Supported by Java such as int, long, char, boolean, and so on)
  • String
  • CharSequence
  • List(must be one of the supported data types or parcelables)
  • Generic List such as List<String> supported
  • Map (must be one of the supported data types or parcelables )
  • Generic maps, such as Map<String,Integer> are not supported
# Defining the AIDL
  • AIDL syntax is very similar to regular Java interface , Define AIDL interface in an .aidl
  • Then save it in the source code (in the src/ directory) of both the application hosting the service and any other application that binds to the service.
  • When you build each application that contains the .aidl file, the Android SDK tools generate an IBinder interface based on the .aidl file and save it in the project's gen/ directory.
# Steps for creating a bounded service using AIDL

1. Create the .aidl file
This file defines the programming interface with method signatures.
2. Implement the interface
The Android SDK tools generate an interface in the Java programming language, based on your .aidl file. This interface has an inner abstract class named Stub that extends Binder and implements methods from your AIDL interface. You must extend the Stub class and implement the methods.
3. Expose the interface to clients
Implement a Service and override onBind() to return your implementation of the Stub class.

Note:

  1.   Any changes that you make to your AIDL interface after your first App release must remain backward compatible in order to avoid breaking other applications that use your service.
  2. That is, because your .aidl file must be copied to other applications in order for them to access your service's interface, you must maintain support for the original interface.

















In above example, I am using two application Client App and Server App.  Client App will get number from user and send it to server.
Server App will receive number from client and perform addition and send result back to client App.

In my example code App is server App and app2 is client App.
Hope you people enjoy it. Flow Diagram for AIDL.



Note:- Please proved your comment and suggestion for my post. It will energize me.

0 komentar:

Posting Komentar

 
[tutup]