android-using Aidl interprocess communication

Source: Internet
Author: User
Tags final int size requires stub

Communication between the client and the service, although it can be implemented through IBinder, requires a shared business implementation, if the haunted of interprocess communication needs to be performed using Aidl (the Android Interface Definition Language).

Aidl is an interface definition language, which is used to constrain communication rules between two processes, compiler generates code, realizes two interprocess communication (IPC) on Android device, aidl IPC mechanism is similar to CORBA used by EJB, communication information between processes, The first is converted to the AIDL protocol message, which is then sent to the other side, which is converted to the corresponding object after receiving the AIDL protocol message. Since the communication between processes requires two-way conversion, Android uses proxy classes to realize bidirectional conversion of information, the proxy class is generated by the Android compiler, and is transparent to the developer.

The use of the following methods:

1, the definition of aidl (similar to the interface, but no visibility, extension has. java->.aidl)

Idownloadservice.aidl, note the extension

Package cn.itcast.aidl;

Interface idownloadservice{

void Download (in/out/input String path);//in|out|inout is the direction of the parameter.

}

The IDE automatically generates the corresponding Java class under the Gen package, and the interface file generates a stub abstract class, which includes the method defined by Aidl, and includes some other helper methods. Of concern is Asinterface (IBinder ibinder), which returns an instance of the interface type, for remote service invocation, the object that the remote service returns to the client, the client onserviceconnectionted (componentname Name,ibinder Service) method cannot directly be strongly converted to an instance of an interface type while referencing the object, but should use Asinterface (IBinder ibinder) for type conversion.

Writing AIDL requires attention:

1. Interface name and Aidl file are the same.

2. Interface and method without access modifier public,private,protected, and can not use final,static.

3. Aidl default supported types include Java base types (Int,long,boolean, etc.) and (string,list,map,charsequence), which are not required with import declarations. The element types in the list and map must be aidl supported types, and if the custom type is used as a parameter or return value, the custom type must implement the Parcelable interface.

4. Other interface types generated by Aidl of custom types in the aidl description file, import should be shown, even in the same package as the type and the defined package.

5. All non-Java base type parameters in the Aidl file must be added in, out, and inout tags to indicate whether the parameter is an input parameter, an output parameter, or an input or output parameter.

6.Java the original type default tag bit in, cannot be a different tag.

JavaBean must implement Parcelable interface

Class Person implements parcelable{

Id,name;

public int describecontents () {

return 0;

}

Write the data in the JavaBean to parcel

public void Writetoparcel (Parcel dest,int flags) {

Dest.writeint (this.id);

Dest.writestring (this.name);

}

Adds a static member, named Creator, that implements the Parcelable.creator interface

public static final parcelable.creator<person> Creator = new Parcelable.creator<person> () {

Public person Createfromparcel (Parcel source) {return new person (Source.readint (), source.readstring ());

}

Public person[] NewArray (int size) {

return new Person[size];

}};

}

To define a aidl declaration file in a custom type package

Person.aidl, note that parcelable is lowercase.

Package Cn.itcast.domain;

Parcelable person;

Interface Cn.itcast.domain.Person;

Interface ipersonservice{

void save (in person);

}

Create a Aidl interface implementation class (implemented by inheriting ${business interface}.stub Class)

public class Servicebinder extends ipersonservice.stub{

public void Save (person) throws remoteexception{

LOG.I ("Personservice", Person.getid () + "=" +person.getname ());

}

}

To implement the Onbind method of the service, the return value is the Aidl implementation class object that was created in the previous step.

Public IBinder Onbind (Intent Intent) {

return new Servicebinder ();

}

The client accesses the service implicitly through an implicit intent.

<service android:name= ". Personservice ">

<intent-filter>

<action android:name= "Cn.itcast.process.aidl.PersonService"/>

New Intent ("Cn.itcast.process.aidl.PersonService");

Copy the Aidl file and the package to the client's corresponding SRC. (the client will automatically generate the corresponding Java class)

This.bindservice (, this.sc,bind_auto_create);

sc = new Serviceconnection () {

public void onserviceconnected (Componentname,ibinder service) {

Personservice = IPersonService.Stub.asInterface (service);

Personservice.save (New person ("liming"));

}

public void onservicedisconnected (componetname name) {

Personservice = null;

}

}

Android-End of Call

Android does not have an API for public calls, and if you need to end the call, you must use AIDL on the phone Management service and call the API in the service to end the call, as follows:

1. Copy the following files from the Android source code to the project:

Com/android/internal/telephony/itelephony.aidl

Android/telephony/neighboringcellinfo.aidl

As shown in the picture on the right. The development tool will automatically generate Itelephony.java in the Gen directory

2. Call Itelephony.endcall () End Call:

Method method = Class.forName ("Android.os.ServiceManager")

. GetMethod ("GetService", String.class);

IBinder binder = (ibinder) method.invoke (Null,new object[]{telephony_service});

Itelephony telephony = ITelephony.Stub.asInterface (binder);

Telephony.endcall ();

Adding permissions in the manifest file Androidmanifest.xml

<uses-permission android:name= "Android.permission.CALL_PHONE"/
>

Related Article

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.