How to define AIDL for cross-process communication

Source: Internet
Author: User
Tags stub

When process A is going to call the service in process B and implement communication, we usually do it through aidl.

Project A:

First in our own package com.wzp.aidlservice Create a remoteservice.aidl file in which we customize an interface that contains the method GetService (). adt plug-ins automatically generate a Remoteservice.java in the Gen directory File, which contains an inner class named Remoteservice.stub that contains the GetService () method of the Aidl file interface

For example:

Package Com.wzp.aidlservice;interface Remoteservice {      String getService ();}


Description One: The location of the Aidl file is not fixed, can be arbitrary, and do not use modifiers in the definition interface (Private,public these)

Next, define your own MyService class, in the MyService class, define an inner class to inherit the remoteservice.stub inner class and implement the GetService method. Returning an object of this inner class in the Onbind method, the system automatically encapsulates the object as a IBinder object and passes it to its caller.

For example:

public class MyService extends Service {public class Myserviceimpl extends Rometeservice.stub {@Override public String g Etservice () throws RemoteException {return "aidl example";}} @Override public IBinder onbind (Intent Intent) {return new Myserviceimpl ();}}
in the Onbind method, it is important to return an object instance of the Myserviceimpl class, otherwise the caller is not getting the service object

Then, you need to configure the MyService class in the Androidmanifest.xml file, with the following code:

<!--registration Service--><service android:name= ". MyService ">  <intent-filter>   <!--Specify the ID of the AIDL service to invoke--      <action android:name=" Com.wzp.aidlservice.RemoteService "/>   </intent-filter></service>

Specifies that the ID of the AIDL service is called to tell the outside world (other processes) that myservice this class can be accessed by another process. As long as the other process knows the ID, it will be able to communicate. In this case, process B can find process A for communication.

Note: Aidl does not require permissions


Project B:

First we will copy the Remoteservice.java file generated in project A into Project B and bind the Aidl in the Bindservice method. The service binding Aidl service is the Remoteservice ID as the intent action parameter.

Description: If we put the Remoteservice.aidl file in a package separately, we will copy the package from the Gen directory to project B. If we store the Remoteservice.aidl file with our other classes, we will create the appropriate package in Project B to ensure that the package name of the Rmoteservice.java file is correct, and we cannot modify the Remoteservice.java file

bindservice (New Intent ("com.wzp.aidlservice . Remoteservice "), serviceconnection,context.bind_auto_create);

serviceconnection onserviceconnected (componentname name,ibinder Service) The service parameter in the MyService method is an object in project A that inherits the inner class of the Remoteservice.stub class.



How to define AIDL for cross-process communication

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.