How to use Android aidl

Source: Internet
Author: User

first, what is the Aidl serviceServices that are typically created cannot be accessed by other applications. To enable other applications to access the services provided by the application, the Android system is implemented using remote procedure calls (Procedure Call,rpc). Like many other RPC-based solutions, Android uses an interface definition language (Interface definition Language,idl) to expose the interfaces of the service. As a result, this service that can be accessed across processes is referred to as the Aidl (Android Interface Definition Language) service.ii. Basic Grammar of Aidl

Aidl uses simple syntax to declare an interface, describe its methods, and the parameters and return values of its methods. These parameters and return values can be any type, or even an interface generated by other aidl.
The basic data types for the Java programming language (int, long, char, boolean,string,charsequence) are set interface types list and map, and import statements are not required.
If you need to use other Aidl interface types in Aidl, import is required, even under the same package structure. Aidl allows you to pass a class that implements the Parcelable interface, requiring an import
It is important to note that for non-basic data types, nor for string and charsequence types, directional indications are required, including in, out, and Inout,in, which are set by the client, which is set by the server, and InOut is both.

Aidl only supports interface methods and cannot expose static variables.

third, the Android application layer uses Aidl3.1. Brief steps 1. Create a file with the extension a.aidl in the Java package directory of the Eclipse Android Project and write down the required interface. If the contents of the Aidl file are correct, ADT will automatically generate a A.java interface file in the Gen directory.   
2. Establish a service class (subclass of Services). and create an inner class in the created service class that implements the Java interface generated by the Aidl file.
3. When the Onbind method of the service class returns, the inner class object that implements the Aidl interface is returned.
4. Configure the Aidl service in the Androidmanifest.xml file, especially note that the attribute value Android:name in the,<action> tag is the ID of the client to reference the service, which is the parameter value of the intent class. 3.2, the specific operation3.2.1, create file Imyservice.aidl:


File contents:

    1. Package du.pack;
    2. Interface imyservice{
    3. //Only one interface
    4. String GetValue ();
    5. }
3.2.2, creating service classes and implementing internal classes
  1. Public class MyService extends Service {
  2. @Override
  3. Public IBinder Onbind (Intent arg0) {
  4. //Return the object of the inner class to the client for use
  5. return new Myserviceimpl ();
  6. }
  7. //Create an internal class that inherits from Imyservice.stub
  8. public class Myserviceimpl extends Imyservice.stub {
  9. //must implement interfaces in the Aidl file
  10. Public String GetValue () throws remoteexception {
  11. return null;
  12. }
  13. }
  14. }
Note that in the service we write, the Onbind method must return an object instance of the Myserviceimpl class, or the client cannot obtain the service object.
3.2.3, configuring the MyService class in the Androidmanifest.xml file
    1. <service android:name=>  
    2.      <intent-filter>  
    3.         action android:name= " Du.pack.IMyService " />  
    4.      </intent-filter>   
    5. </service >  

The "Du.pack.IMyService" above is the ID that the client uses to access the AIDL service.

4. How to use the local client4.1. Create a new Eclipse Android project and copy the Imyservice.java file under the Gen directory that you just generated from the remote server to the SRC directory of the client project together with the package directory.

4.2. Call the Aidl service first to bind the service before obtaining the service object.
  1. Public class Aidlclienttestactivity extends Activity {
  2. //objects on the remote server
  3. Imyservice Mimyservice;
  4. private Serviceconnection mconnection = new Serviceconnection () {
  5. public void onserviceconnected (componentname name, IBinder service) {
  6. //Bind successfully, get the object of remote server, Target complete!!!
  7. Mimyservice = IMyService.Stub.asInterface (service);
  8. }
  9. public void onservicedisconnected (componentname name) {
  10. //Unbind
  11. Mimyservice = null;
  12. }
  13. };
  14. @Override
  15. public void OnCreate (Bundle savedinstancestate) {
  16. super.oncreate (savedinstancestate);
  17. Setcontentview (R.layout.main);
  18. //Bind remote service side service
  19. Intent serviceintent = new Intent ("Du.pack.IMyService");
  20. Bindservice (Serviceintent, mconnection, context.bind_auto_create);
  21. }
  22. }
5. Summary of Usage Review the procedure for the entire invocation:  
Server side: Abstract the required open interface into the Aidl file, then implement the interface in its own inner class, and return the inner class object to the client when it is bound. Client side: When we need to remote a service, just like in the local service to Bindservice, and then in the binding successful callback function (that is, the Onserviceconnected method) Get a IBinder object (such as service), then call IMyService.Stub.asInterface (service) Such a statement, you can get the server open Interface interface object, At this point the client can invoke the method of the object directly, as if invoking the remote service object directly.

Iv. use of aidl in the framework

Using Aidl in the framework we analyze this systemservice through Itelephonyregistry. The main role of the service is to listen to the call-related events, we focus on the implementation of AIDL structure, do not pay attention to itelephonyregistry specific implementation.

1. aidl File Related

First look at the Aidl file for this service:

  1. @ITelephonyRegistry. Aidl
  2. Interface Itelephonyregistry {
  3. void Listen (String pkg, Iphonestatelistener callback, int events, boolean notifynow);
  4. void notifycallstate (int state, String incomingnumber);
  5. void Notifyservicestate (in servicestate state);
  6. void Notifysignalstrength (in Signalstrength signalstrength);
  7. void Notifymessagewaitingchanged (boolean MWI);
  8. }

then look at the real implementation of this service:

  1. @TelephonyRegistry. java
  2. Class Telephonyregistry extends Itelephonyregistry.stub {
  3. Telephonyregistry (Context context) {
  4. ......
  5. }
  6. void Listen (String pkg, Iphonestatelistener callback, int events, boolean notifynow) {
  7. ......
  8. }
  9. void notifycallstate (int state, String incomingnumber) {
  10. ......
  11. }
  12. void Notifyservicestate (in servicestate state) {
  13. ......
  14. }
  15. void Notifysignalstrength (in Signalstrength signalstrength) {
  16. ......
  17. }
  18. void Notifymessagewaitingchanged (boolean mwi) {
  19. ......
  20. }
  21. }

The above two files are a core part of the service, and the Aidl file specifies the functionality of the service, while the Java file is the specific implementation of the feature. However, at this point the telephonyregistry does not inherit the service's class, that is, currently he does not qualify as a service. So how did he become a service?

2, the registration process of the service

The answer can be found in the Systemservice.

  1. @SystemServer. java
  2. Class Serverthread extends Thread {
  3. @Override
  4. public Void Run () {
  5. try {
  6. Telephonyregistry = New Telephonyregistry (context);
  7. Servicemanager.addservice ("Telephony.registry", telephonyregistry);
  8. }
  9. }
  10. }

We see that in this step, the Telephonyregistry object (that is, the subclass object of Itelephonyregistry.stub) is registered as a service to the ServiceManager. and the registered name is "Telephony.registry".
With this step, Telephonyregistry can be opened to the client as a service provider. In other words, with this step, Telephonyregistry is counted as a real service that can accept client connection requests.

So then, how do we get the service?

3, how to get a registered service

Since the service is registered through ServiceManager, we need to get its service object again through ServiceManager.

    1. Private Itelephonyregistry sregistry;
    2. Sregistry = ITelephonyRegistry.Stub.asInterface (Servicemanager.getservice ("Telephony.registry"));

In this way, we get the Itelephonyregistry.aidl object sregistry.

4. Process Summary

Review the framework for this AIDL service: Implement the interface specified in Aidl by inheriting the Itelephonyregistry.stub parent class, and then register Telephonyregistry as ServiceManager to Systemservice. The client can get the Telephonyregistry object through ServiceManager, and then it can invoke the interface defined in Aidl.

How to use Android aidl

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.