AIDL - Android Interface Definition Language

4,646 views
4,279 views

Published on

Modified example from Marakana. Using AIDL Service to authenticate username and password

Published in: Technology

AIDL - Android Interface Definition Language

  1. 1. AIDL - Services and IPC
  2. 2. Inter Process Communication
  3. 3. The caller invokes Proxy. Proxy inturn binds the current activity to the remote service This is the Remote method. It is exposed as a Bound Service
  4. 4. droid Services - Basics ervice is an application component that can perform long­running operations in th kground and does not provide a user interface. ervice can essentially take two forms: rted : A service is "started" when a component (such as an activity) starts it by ing startService(). For example, it might download or upload a file over the networ und : A service is "bound" when an application component binds to it by calling dService(). A bound service offers a client­server interface that allows components nteract with the service, send requests, get results, and even do so across cesses with interprocess communication (IPC).
  5. 5. droid Services – Basics reate a service, you must create a subclass of Service . back methods you should override are: nd() : The system calls this method when another component wants to bind with t ice (such as to perform RPC), by calling bindService(). you must provide an interface that clients use to communicate with the service eturning an IBinder. reate() : The system calls this method when the service is first created, to perform ­time setup procedures (before it calls either onStartCommand() or onBind()). estroy() : The system calls this method when the service is no longer used and is g destroyed.
  6. 6. IDL – Android Interface Definition Language DL (Android Interface Definition Language) allows you to define the ogramming interface that both the client and service use to communicate using Android, one process cannot normally access the memory of another process. to talk, they need to decompose their objects into primitives that the erating system can understand, and marshall the objects across that boundary you.
  7. 7. vice Life Cycle
  8. 8. ypical Use Case
  9. 9. xample ACTIVITY Username, password Activity User details Activity Activity Remote Service
  10. 10. teps Create the .aidl file : This file defines the interface with method signatures. Implement the interface : The Android SDK tools generate an interface 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 mus extend the Stub class and implement the methods. Expose the interface to clients : Implement a Service and override onBind() to return your implementation of the Stub class.
  11. 11. Creating a .aidl file // Declare the interface. – IUserInfoService.aidl interface IUserInfoService { String getInfo(in String user, in String pass); } hen you build your application, the SDK tools generate a .java interface file named after your .aidl e. he generated interface IUserInfoService contains inner class Stub ­ IUserInfoService.Stub
  12. 12. . Implementing the interface eed to extend this class IUserInfoService.Stub by providing additional methods  return new IUserInfoService.Stub() { @Override public String getInfo(String user, String pass) throws RemoteException { // TODO Auto­generated method stub if (user.equals("arvind")) return "Welcome Arvind! You have 15 unread messages"; return user+pass; }
  13. 13. Exposing your interface in Service class e you've implemented the interface for your service,  need to expose it to clients so they can bind to it.  // UserInfoService public IBinder onBind(Intent intent) { return new IUserInfoService.Stub() { @Override public String getInfo(String user, String pass) throws RemoteException { // TODO Auto­generated method stub if (user.equals("arvind")) return "Welcome Arvind! You have 15 unread messages"; return user+pass; } };
  14. 14. ccessing Service in Activity Use ServiceConnection class � onServiceConnected gives reference to service interface service = IUserInfoService.Stub.asInterface((IBinder) boundService Access the methods of service service.getUserInfo(“….”,”….”);
  15. 15. dditional Info The bindService() method returns immediately without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected( on the ServiceConnection, to deliver the IBinder that the clie can use to communicate with the service.
  16. 16. ��������������������������������������������������������������������������� ��������������������������������������������������������������������������������� �����������������������������������������������������

×