<span id="Label3"></p>1. There is no sharing of memory between processes in the aidlandroid system, so there are mechanisms that need to be provided for data communication between different Processes. 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. We know that 3 of the 4 Android application components (Activity, broadcastreceiver, and contentprovider) can be accessed across processes, and another Android application component service is also available. As a result, this service that can be accessed across processes is referred to as the aidl (Android Interface Definition Language) service. 2. Steps to establish a AIDL service it is more complex to establish a aidl service than to establish a common service, as follows: (1) create a file with the extension aidl in the Java package directory of the Eclipse Android Project. The syntax of the file is similar to Java code, but slightly different. (2) if the contents of the Aidl file are correct, ADT will automatically generate a Java interface file (*.java). (3) set up a service class (subclass of services). (4) Implement the Java interface generated by the Aidl File. (5) 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. typically, Each application runs within his own process, but sometimes objects need to be passed between processes (IPC communication). At this point, you can write a service running in a different process through the application Ui. In the Android platform, a process typically does not have access to memory areas in other Processes. So they need to split the object into simple forms that the operating system can understand so that it is disguised as an object Cross-border access. And to complete these needs aidl Mechanism.<p><p>Aidl (android Interface Description Language) is an IDL language that can generate a piece of code that can be used to interact with an internal communication process with two processes running on an Android device. If you want to access a method of an object in another process, such as a service, in a process, such as in an activity, you can use AIDL to generate such code to disguise the passing of various parameters.</p></p><p><p>To use Aidl,service to provide the service interface as a aidl file, the Aidl tool generates a corresponding Java interface and includes a stub () service pile class for the function call in the generated server interface. The Onbind method of the service returns the object of the implementation class, and you can then use it.</p></p><p><p>Aidl file</p></p><p><p>The aidl contained in the framework are defined in the Frameworks/base/android.mk. This file defines two lists of Aidl Files.</p></p><p><p>The first is to assign the value "+ =" to the Local_src_files variable, which will contain all the source files in the Framework.jar target, including the Aidl file and the Java File.</p></p><p><p>The second is to assign a value to the Aidl_files variable using the ": =" assignment symbol, which contains only the Aidl files in the Android.jar target.</p></p><p><p>therefore, when adding a new Aidl file to frameworks, you need to consider whether the file should be exposed to the SDK. If necessary, you need to add the file path to the above two variables at the same time, and if you do not need to expose it to the sdk, you only need to add the file path to the Local_src_files Variable.</p></p><p><p></p></p><p style="text-align: left; line-height: 26px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 15px; margin-top: 0px; margin-bottom: 0px;"><p style="text-align: left; line-height: 26px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 15px; margin-top: 0px; margin-bottom: 0px;">After completing these operations, the compilation will still have a problem, the need to run, make Update-api command, at this time change the file is Current.txt changed content as Follows:</p></p> <blockquote style="margin: 0px 0px 0px 40px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial; font-size: 15px;"> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ Public Abstract Interface Ieneaservice implements Android.os.IInterface {</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ }</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ public static abstract class Ieneaservice.stub extends Android.os.Binder implements Android.os.IEneaService {</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ ctor public ieneaservice.stub ();</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ method public Android.os.IBinder Asbinder ();</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ method public static Android.os.IEneaService Asinterface (android.os.IBinder);</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ Method Public boolean ontransact (int, android.os.Parcel, android.os.Parcel, Int) throws android.os.RemoteException;</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+ }</p></p> <p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;"><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0px;">+</p></p> </blockquote><p><p><span style="text-align: left; line-height: 26px; font-family: Arial; font-size: 15px;">In this case, if you want to use the activity in writing, you need to get the service first, This step is implemented through the API "servicemanager.getservice ()". The service handle is then used to invoke the function exposed by the SERVICE. Itestservice om = ITestService.Stub.asInterface (servicemanager.getservice ("Test"));</span></p></p><p><p><span style="text-align: left; line-height: 26px; font-family: Arial; font-size: 15px;">Here's a simple example:</span></p></p><p><p></p></p><pre><pre>/** helloserver.java*/package COM. Test.helloserver;import Android.app.activity;import Android.os.bundle;import Android.os.servicemanager;import Android.os.itestservice;import Android.util.log;public class HelloServer extends Activity { private static final String DTAG = "helloserver"; /** called when the activity is first Created. * /@Override public void onCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Itestservice om = ITestService.Stub.asInterface (servicemanager.getservice ("Test")); Try { log.d (DTAG, "going to call service"); Om.setvalue (a); LOG.D (DTAG, "Service called succesfully"); } Catch (Exception e) { log.d (DTAG, "FAILED to call service"); E.printstacktrace ();}}} </pre></pre><p><p>Aidl (Android Interface Definition languageandroid Interface definition Language)</p></p></span>
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