Android interprocess communication aidl

Android interprocess communication aiDL multi-client, multi-threaded, IPC \

Step 1: Create an itestaidl.aidl file

Eclipse automatically generates a corresponding itestaidl.java file in the Gen folder next time

Step 2: Create a service,

A binder object returned inherits ITestAidl.Stub

Step 3: On the client side, bind the service and get the IBinder object through ServiceConnection.

Get data from the server

\

\

Service returns an IBinder object that implements stubs in the AIDL interface (inheriting IBinder)

The client binds the Service and obtains the IBinder from the Service through ServiceConnection.

The client transmits the data and returns the results,

\

Aidl low-level implementation

\

\

Client-side implementation: \

package com.android.hellosumaidl;

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;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;

public class HelloSumAidlActivity extends Activity {

	IAdditionService service;
	AdditionServiceConnection connection;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		initService();
		
		Button buttonCalc = (Button)findViewById(R.id.buttonCalc);
		
		buttonCalc.setOnClickListener(new OnClickListener() {
			TextView result = (TextView)findViewById(R.id.result);
			EditText value1 = (EditText)findViewById(R.id.value1);
			EditText value2 = (EditText)findViewById(R.id.value2);
			
			@Override
			public void onClick(View v) {
				int v1, v2, res = -1;
				v1 = Integer.parseInt(value1.getText().toString());
				v2 = Integer.parseInt(value2.getText().toString());
				
				try {
					res = service.add(v1, v2);
				} catch(RemoteException e) { e.printStackTrace(); } result.setText(Integer.valueOf(res).toString()); }}); }@Override
	protected void onDestroy(a) {
		super.onDestroy();
		releaseService();
	}
	
	/* * This inner class is used to connect to the service */
	class AdditionServiceConnection implements ServiceConnection {
		public void onServiceConnected(ComponentName name, IBinder boundService) {
			service = IAdditionService.Stub.asInterface((IBinder)boundService);
			Toast.makeText(HelloSumAidlActivity.this."Service connected", Toast.LENGTH_LONG).show();
		}
		
		public void onServiceDisconnected(ComponentName name) {
			service = null;
			Toast.makeText(HelloSumAidlActivity.this."Service disconnected", Toast.LENGTH_LONG).show(); }}/* * This function connects the Activity to the service */
	private void initService(a) {
		connection = new AdditionServiceConnection();
		Intent i = new Intent();
		i.setClassName("com.android.hellosumaidl", com.android.hellosumaidl.AdditionService.class.getName());
		boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
	}
	
	/* * This function disconnects the Activity from the service */
	private void releaseService(a) {
		unbindService(connection);
		connection = null; }}Copy the code

\

\

\

Add iadditionService.java generated by.aidl in gen folder

Copy the file to have a look:

\

\

public interface IAdditionService extends android.os.IInterface
{
	/** Local-side IPC implementation stub class. */
	public static abstract class Stub extends Binder implements IAdditionService
	{
		private static final String DESCRIPTOR = "com.android.hellosumaidl.IAdditionService";
		/** constructor. */
		public Stub(a)
		{
			this.attachInterface(this, DESCRIPTOR);
		}
		/** Create an IBinder object when the proxy is needed */
		public static IAdditionService asInterface(IBinder obj)
		{
			if ((obj==null)) {
				return null;
			}
			IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
			if(((iin! =null)&&(iin instanceof IAdditionService))) {
				return ((IAdditionService)iin);
			}
			return new IAdditionService.Stub.Proxy(obj);
		}
		@Override public  IBinder asBinder(a)
		{
			return this;
		}
		@Override public boolean onTransact(int code, 
				Parcel data, Parcel reply, int flags) 
						throws android.os.RemoteException
		{
			switch (code)
			{
			case INTERFACE_TRANSACTION:
			{
				reply.writeString(DESCRIPTOR);
				return true;
			}
			case TRANSACTION_add:
			{
				data.enforceInterface(DESCRIPTOR);
				int _arg0;
				_arg0 = data.readInt();
				int _arg1;
				_arg1 = data.readInt();
				int _result = this.add(_arg0, _arg1);
				reply.writeNoException();
				reply.writeInt(_result);
				return true; }}return super.onTransact(code, data, reply, flags);
		}
		private static class Proxy implements IAdditionService
		{
			private IBinder mRemote;
			Proxy(android.os.IBinder remote)
			{
				mRemote = remote;
			}
			@Override public android.os.IBinder asBinder(a)
			{
				return mRemote;
			}
			public java.lang.String getInterfaceDescriptor(a)
			{
				return DESCRIPTOR;
			}
			// You can pass the value of in, out or inout
			// The primitive types (int, boolean, etc) are only passed by in

			@Override public int add(int value1, int value2) throws android.os.RemoteException
			{
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				int _result;
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					_data.writeInt(value1);
					_data.writeInt(value2);
					mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
					_reply.readException();
					_result = _reply.readInt();
				}
				finally {
					_reply.recycle();
					_data.recycle();
				}
				return_result; }}static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
	}
	// You can pass the value of in, out or inout
	// The primitive types (int, boolean, etc) are only passed by in

	public int add(int value1, int value2) throws android.os.RemoteException;
}
Copy the code


\