A good understanding of aidl

Source: Internet
Author: User

Android service is also used a lot at work, but aidl is rarely used, and it feels very unfamiliar. Some colleagues in the company also gave technical lectures and read some technical articles, but it still seems to be too busy. Now I have read aidl again after teaching and found that it is not as complicated as understanding. It is actually a very simple RPC mechanism.

Before using aidl, you must exchange data or call methods with the services of other applications. (Remote operations on other services ). Otherwise, if there is no data exchange or method call, you can directly use startservice (). The local service does not require aidl.

 

After clarifying this premise, let's look at the knowledge points involved in aidl:

1. serviceconnection interface: After this interface is implemented, the callback methods of service connection status can be onserviceconnected (componentname component, ibinder service) and onservicedisconnected (componentname component ).

2. ibinder Interface

3. Binder class: binder can be considered as a memory shared object. This object is only exposed to the client for calling.

4. stub static internal class: in fact, it is a class (so-called proxy) that inherits the binder and implements its own defined aidl interface, and is a binder that implements its own defined interface.

5. Implement the custom Java Bean Of The parcelable serialization interface: it is unnecessary to transmit simple data. (By the way: 1. data must be serialized to a disk or transmitted over the network; 2. the most commonly used bundle is actually a parcelable, which can be used to understand parcelable)

Note the following when using aidl:

1. The file name must end with. aidl

2. The package name of the aidl file is very important. The package name must be consistent between the service end and the call end.

 

First, the following figure shows how the entire call process and remote process call work:

The following shows the project code structure:

The code below:

1. iperson. aidl file:

[Java]
  1. Package com. wenix. Service. aidl;
  2. /**
  3. * In addition to the basic types, such as string, list, map, and charsequence, the package must be imported for other types. The package is instantly under the same package.
  4. */
  5. Import com. wenix. Service. aidl. person;
  6. Interface iperson {
  7. Person getperson (in int ID );
  8. }

2. Person. aidl file: (used to declare serialized objects)

[Java]
  1. Package com. wenix. Service. aidl;
  2. /**
  3. * The person type is declared as parcelable in lower case.
  4. */
  5. Parcelable person;


3. personservice. Java file: (define the service that provides the service. Remember to register it in the androidmenifest. xml file)

[Java]
  1. Package com. wenix. androidaidl;
  2. Import com. wenix. Service. aidl. iperson;
  3. Import com. wenix. Service. aidl. person;
  4. Import Android. App. Service;
  5. Import Android. content. intent;
  6. Import Android. OS. ibinder;
  7. Import Android. OS. RemoteException;
  8. Public class personservice extends Service {
  9. // Because it is a remote service, the client certainly cannot start the service using the display intent, so action must be provided.
  10. Public static final string aidl_action = "com. wenix. Intent. Action. aidl_action ";
  11. Private personbinder;
  12. /**
  13. * It inherits the automatically generated stub static internal class and provides the implementation of interface methods.
  14. * @ Author wenix
  15. */
  16. Public class personbinder extends iperson. Stub {
  17. @ Override
  18. Public Person getperson (int id) throws RemoteException {
  19. // Only one simple custom object is returned here. The principle is the same. Any complicated object can be returned if it is serialized.
  20. Return new person (ID, "wenix", "wenix ");
  21. }
  22. }
  23. @ Override
  24. Public ibinder onbind (intent ){
  25. // Return the binder object (memory shared object) provided to the client)
  26. Return personbinder;
  27. }
  28. @ Override
  29. Public void oncreate (){
  30. Super. oncreate ();
  31. // Initialize the data
  32. Personbinder = new personbinder ();
  33. }
  34. }


4. client code: (bind a remote service, obtain the binder object, and then perform data exchange and method calls)

[Java]
  1. Package com. wenix. androidclient;
  2. Import Android. App. activity;
  3. Import Android. content. componentname;
  4. Import Android. content. context;
  5. Import Android. content. intent;
  6. Import Android. content. serviceconnection;
  7. Import Android. OS. Bundle;
  8. Import Android. OS. ibinder;
  9. Import Android. OS. RemoteException;
  10. Import Android. util. log;
  11. Import Android. View. view;
  12. Import Android. View. View. onclicklistener;
  13. Import Android. View. viewgroup. layoutparams;
  14. Import Android. widget. Button;
  15. Import Android. widget. linearlayout;
  16. Import Android. widget. textview;
  17. Import com. wenix. Service. aidl. iperson;
  18. Public class mainactivity extends activity {
  19. Private Static final string tag = "mainactivity ";
  20. Private class personserviceconnection implements serviceconnection {
  21. @ Override
  22. Public void onserviceconnected (componentname CMP, ibinder Service ){
  23. // The client obtains the binder object in this callback method.
  24. Iperson P = iperson. stub. asinterface (service );
  25. Try {
  26. // Call the API specified by aidl to obtain data.
  27. Log. I (TAG, P. getperson (1). tostring ());
  28. } Catch (RemoteException e ){
  29. E. printstacktrace ();
  30. }
  31. }
  32. @ Override
  33. Public void onservicedisconnected (componentname name ){
  34. }
  35. }
  36. @ Override
  37. Public void oncreate (bundle savedinstancestate ){
  38. Super. oncreate (savedinstancestate );
  39. // Dynamic Layout is adopted on the Interface
  40. Linearlayout LL = new linearlayout (this );
  41. Ll. setorientation (linearlayout. Vertical );
  42. Ll. setlayoutparams (New linearlayout. layoutparams (layoutparams. match_parent, layoutparams. match_parent ));
  43. Layoutparams Params = new linearlayout. layoutparams (layoutparams. match_parent, layoutparams. wrap_content );
  44. Button BTN = new button (this );
  45. BTN. setlayoutparams (Params );
  46. BTN. settext ("getting service data ");
  47. BTN. setonclicklistener (New onclicklistener (){
  48. @ Override
  49. Public void onclick (view v ){
  50. // Start the corresponding service. Because it is a remote service, intent cannot be used.
  51. Intent service = new intent ("com. wenix. Intent. Action. aidl_service ");
  52. Personserviceconnection conn = new personserviceconnection ();
  53. Bindservice (Service, Conn, context. bind_auto_create );
  54. }
  55. });
  56. Textview TV = new textview (this );
  57. TV. setlayoutparams (Params );
  58. Ll. addview (BTN );
  59. Ll. addview (TV );
  60. Setcontentview (LL );
  61. }
  62. }

 

After the project is completely completed, click the button to view the obtained data:

You only need to master the entire process, and aidl can be easily used. Some specific instances using aidl will be added later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.