Android Getting Started: interprocess communication through Aidl

Source: Internet
Author: User
Tags gettext stub

First, Aidl introduction
Aidl:android Interface definition Language, Interface Definition language, as the name implies, is the language that defines the interface, that is, the interface can be defined using Aidl;
Aidl is simply a method of interprocess communication, similar to the RMI in Java;
Aidl uses the Xxx.aidl file to define the interface, usually placing the file in Com.xiazdong.aidl;
Aidl files are written similar to the authoring of interfaces, but there are some differences:
(1) No access modifiers (such as public, private, static, final) can be added before the interfaces and methods in Aidl; (2) Aidl supports method parameters in Java base type (3) Aidl If it is a non-Java basic type, add in, Out, InOut, (4) Aidl for the custom type, you need to implement the Parselable interface;
Aidl will automatically generate Java files in the Gen directory after writing;
In the automatically generated files we need to understand the methods are: (1) Xxx xx = Xxx.stub.asInterface (IBinder); //Convert IBinder type to AIDL type, used in Serviceconnection onserviceconnected;(2) Xxx.stub.fun (); //Call Aidl defined method

It should be noted that since the Aidl file is a bridge between the two applications, this document should exist in both applications.


second, the code case

Scene Description: This scenario is the same as the application function in the "Getting Started with Android: Binding to Service" article, where the addition service is applied in the Aidlservice, and the program that invokes the addition is in the Aidlclient application;
The effect is the same as before:



the different organizational structure of the program:



aidlclient Code

Addop.aidl
Package com.xiazdong.aidl;

Interface Addop {	//except without access, the other and interface definitions are almost
	int addService (int a,int b);
}

Mainactivity.java
Package com.xiazdong.aidlclient;

Import com.xiazdong.aidl.AddOp;
Import android.app.Activity;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;

Import Android.widget.TextView;
	public class Mainactivity extends activity {private EditText op1,op2;
	Private button button;
	Private TextView result;
	Private Addop Binder;
	Private Serviceconnection conn = new addopserviceconnection ();
			Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View v) {int number = 0; try {number = Binder.addservice (Integer.parseint (Op1.gettext (). toString ()), Integer.parseint (Op2.gettext (). Tostrin
			g ()));
			catch (Exception e) {e.printstacktrace (); } result.settext ("ResUlt= "+number+");
	}
	};
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.main);
        OP1 = (edittext) This.findviewbyid (R.ID.OP1);
        OP2 = (edittext) This.findviewbyid (R.ID.OP2);
        result = (TextView) This.findviewbyid (R.id.result);
        Button = (button) This.findviewbyid (R.id.button);
        Button.setonclicklistener (listener);	Intent service = new Intent ("Com.xiazdong.action");
    Implicit Intent This.bindservice (service, Conn, bind_auto_create); Private class Addopserviceconnection implements serviceconnection{@Override public void onserviceconnected (Compone Ntname name, IBinder service) {binder = AddOp.Stub.asInterface (service);//convert IBinder to interface type} @Override public V
		OID onservicedisconnected (componentname name) {binder = null; }
	}
}


Aidlservice Code

Addop.aidl
Package com.xiazdong.aidl;

Interface Addop {
	int addService (int a,int b);
}


Addopservice.java
Package com.xiazdong.aidlservice;

Import com.xiazdong.aidl.AddOp;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import android.os.RemoteException;

public class Addopservice extends Service {
	private IBinder binder = new Addopbinder ();
	@Override public
	IBinder onbind (Intent Intent) {return
		binder;
	}
	public int Add (int a,int b) {//service-provided method is provided to the external return a+b via the binder object
		;
	}
	Private class Addopbinder extends addop.stub{	//inheritance aidl generated stub
		@Override public
		int AddService (int a, int b ) throws RemoteException {	//Implement this method return
			Add (a,b);	method to invoke service
		}}}











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.