Aidl custom data types for communication between Android processes

(1) User-defined data type, must realize Parcelable interface and create corresponding data. Aidl

Rewrite writeToParcel(Parcel paramParcel, int paramInt) to write Parcel

Create a Creator < Person > Creator

The createFromParcel(Parcel paramParcel) command is used to read the Parcel

Note that the read and write order is consistent

(2).aidl, which defines the operation method

(3) Create a service

(4) Realize the client

Person. Parcelable \ Java implementation

package com.android.hellosumaidl;

import android.os.Parcel;
import android.os.Parcelable;

public class Person implements Parcelable{
	private String name;
	private int age;
	
	
	public  Person(a){}@Override
	public String toString(a) {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	public Person(String name, int age) {
		super(a);this.name = name;
		this.age = age;
	}
	public String getName(a) {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge(a) {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
	

	@Override
	public int describeContents(a) {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel paramParcel, int paramInt) {
		paramParcel.writeString(name);
		paramParcel.writeInt(age);
	}
	
	
	public static final Creator<Person> CREATOR = new Creator<Person>() {
		
		@Override
		public Person[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Person[size];
		}
		
		@Override
		public Person createFromParcel(Parcel paramParcel) {
			// TODO Auto-generated method stub
			return newPerson(paramParcel); }};public Person(Parcel source){
		this.name = source.readString();
		this.age = source.readInt(); }}Copy the code

\

Person.aidl the.aidl that defines the data

package com.android.hellosumaidl;

parcelable Person;
Copy the code

IAddtionalService.aidl

package com.android.hellosumaidl;

import com.android.hellosumaidl.Person;

interface IAdditionService {
	 List<Person> add(in Person person);
}
Copy the code

\

Service AdditionService

package com.android.hellosumaidl;



import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
 


public class AdditionService extends Service {
	private List<Person> persons ;
  
	
	@Override
	public IBinder onBind(Intent intent) {
		persons = new ArrayList<Person>();
		 return new IAdditionService.Stub() {
			@Override
			public List<Person> add(Person person) throws RemoteException {
				persons.add(person);
				returnpersons; }}; }}Copy the code

Client-side implementation

ServiceConnection get IBinder

IBinder calls the AIDL remote method

<span style="font-size:18px;">package com.android.hellosumaidl;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
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;

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() {
			 
			
			@Override
			public void onClick(View v) {
			 
			 
				try {
					 List<Person> persons = service.add(new Person("adb".12));
					 
					 Log.i("tag", persons.toString());
				} catch(RemoteException e) { e.printStackTrace(); }}}); }@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;
	}
}</span><span style="font-size:24px;">
</span>
Copy the code








\

\

\

\

\

\

\

\