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]
- Package com. wenix. Service. aidl;
- /**
- * 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.
- */
- Import com. wenix. Service. aidl. person;
- Interface iperson {
- Person getperson (in int ID );
- }
2. Person. aidl file: (used to declare serialized objects)
[Java]
- Package com. wenix. Service. aidl;
-
- /**
- * The person type is declared as parcelable in lower case.
- */
- Parcelable person;
3. personservice. Java file: (define the service that provides the service. Remember to register it in the androidmenifest. xml file)
[Java]
- Package com. wenix. androidaidl;
-
- Import com. wenix. Service. aidl. iperson;
- Import com. wenix. Service. aidl. person;
-
- Import Android. App. Service;
- Import Android. content. intent;
- Import Android. OS. ibinder;
- Import Android. OS. RemoteException;
-
- Public class personservice extends Service {
- // Because it is a remote service, the client certainly cannot start the service using the display intent, so action must be provided.
- Public static final string aidl_action = "com. wenix. Intent. Action. aidl_action ";
- Private personbinder;
- /**
- * It inherits the automatically generated stub static internal class and provides the implementation of interface methods.
- * @ Author wenix
- */
- Public class personbinder extends iperson. Stub {
- @ Override
- Public Person getperson (int id) throws RemoteException {
- // Only one simple custom object is returned here. The principle is the same. Any complicated object can be returned if it is serialized.
- Return new person (ID, "wenix", "wenix ");
- }
- }
- @ Override
- Public ibinder onbind (intent ){
- // Return the binder object (memory shared object) provided to the client)
- Return personbinder;
- }
- @ Override
- Public void oncreate (){
- Super. oncreate ();
- // Initialize the data
- Personbinder = new personbinder ();
- }
- }
4. client code: (bind a remote service, obtain the binder object, and then perform data exchange and method calls)
[Java]
- Package com. wenix. androidclient;
-
- Import Android. App. activity;
- Import Android. content. componentname;
- Import Android. content. context;
- Import Android. content. intent;
- Import Android. content. serviceconnection;
- Import Android. OS. Bundle;
- Import Android. OS. ibinder;
- Import Android. OS. RemoteException;
- Import Android. util. log;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. View. viewgroup. layoutparams;
- Import Android. widget. Button;
- Import Android. widget. linearlayout;
- Import Android. widget. textview;
-
- Import com. wenix. Service. aidl. iperson;
-
- Public class mainactivity extends activity {
- Private Static final string tag = "mainactivity ";
- Private class personserviceconnection implements serviceconnection {
- @ Override
- Public void onserviceconnected (componentname CMP, ibinder Service ){
- // The client obtains the binder object in this callback method.
- Iperson P = iperson. stub. asinterface (service );
- Try {
- // Call the API specified by aidl to obtain data.
- Log. I (TAG, P. getperson (1). tostring ());
- } Catch (RemoteException e ){
- E. printstacktrace ();
- }
- }
- @ Override
- Public void onservicedisconnected (componentname name ){
-
- }
- }
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
-
- // Dynamic Layout is adopted on the Interface
- Linearlayout LL = new linearlayout (this );
- Ll. setorientation (linearlayout. Vertical );
- Ll. setlayoutparams (New linearlayout. layoutparams (layoutparams. match_parent, layoutparams. match_parent ));
-
- Layoutparams Params = new linearlayout. layoutparams (layoutparams. match_parent, layoutparams. wrap_content );
- Button BTN = new button (this );
- BTN. setlayoutparams (Params );
- BTN. settext ("getting service data ");
- BTN. setonclicklistener (New onclicklistener (){
- @ Override
- Public void onclick (view v ){
- // Start the corresponding service. Because it is a remote service, intent cannot be used.
- Intent service = new intent ("com. wenix. Intent. Action. aidl_service ");
- Personserviceconnection conn = new personserviceconnection ();
- Bindservice (Service, Conn, context. bind_auto_create );
- }
- });
- Textview TV = new textview (this );
- TV. setlayoutparams (Params );
- Ll. addview (BTN );
- Ll. addview (TV );
- Setcontentview (LL );
- }
- }
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.