android 进程通信之aidl 自定义数据类型

1,024 阅读1分钟

android 进程通信之aidl 自定义数据类型

(1)自定义数据类型, 必需实现Parcelable接口  并创建对应数据的.aidl

重写writeToParcel(Parcel paramParcel, int paramInt)    写入Parcel 

创建Creator<Person> CREATOR  

提供createFromParcel(Parcel paramParcel)     读出Parcel 

注意读写顺序一致

   (2) 定义操作方法的.aidl

  (3) 创建服务

(4)实现客户端

Person.java  实现Parcelable\

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(){
		
	}
	 
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

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

	@Override
	public int describeContents() {
		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 new Person(paramParcel);
		}
	};
	
	public Person(Parcel source){
		this.name = source.readString();
		this.age = source.readInt();
	}
	
}

\

Person.aidl   定义数据的.aidl

package com.android.hellosumaidl;

parcelable Person;

IAddtionalService.aidl

package com.android.hellosumaidl;

import com.android.hellosumaidl.Person;

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

\

服务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);
				return persons;
			}
		};
	}
}


客户端实现

ServiceConnection 获取IBinder 

IBinder 调用aidl远程方法

<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() {
		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() {
		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() {
		unbindService(connection);
		connection = null;
	}
}</span><span style="font-size:24px;">
</span>





\

\

\

\

\

\

\

\