Saturday, 18 February 2017

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.


Happy Coding !!!

Thanks
Saurabh

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

6 comments:

  1. I really appreciate the information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in #APACHE #CAMEL, kindly Contact MaxMunus
    MaxMunus Offer World Class Virtual Instructor-led training on #APACHE #CAMEL. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 1,00,000 + training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain, and UAE etc.
    Avishek Priyadarshi
    MaxMunus
    E-mail: avishek@maxmunus.com
    Skype id: avishek_2.
    Ph:(0) 8553177744 / 080 - 41103383
    www.MaxMunus.com

    ReplyDelete
  2. With the technology improves, many industries has to be update to beat their competitors. Mobile Application Development Company India is increasing massively due to the immense development and android mobile phone users. This is some extra points to be added from my point of view.
    Regards:
    Android Course in Chennai | Android Training Chennai

    ReplyDelete
  3. Very useful information that you have shared and it is very useful to me.Thanks for sharing the information with us.

    best mobile app development company in chennai

    ReplyDelete
  4. Thanks for sharing this post
    a href="https://hanumanchalisalyricspdf.in/">Hanuman Chalisa Lyrics Pdf
    Hanuman Chalisa Tamil Pdf
    Hanuman Chalisa English Pdf
    Hanuman Chalisa Telugu pdf

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...