首页 > 代码库 > AIDL(2):通过传输复杂对象
AIDL(2):通过传输复杂对象
IPC通过AIDL传递复杂对象
1.定义数据传输对象
Person.aidl文件:
Person.java文件中:
(1)实现parcelable接口
(2)提供一个名为CREATOR的static final属性
package com.liujun.aidl;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable{
private String name;
private int sex;
public Person(){
}
public Person(Parcel source){
readFromParcel(source);
}
//必须提供一个名为CREATOR的static final属性 该属性需要实现android.os.Parcelable.Creator<T>接口
public static final Parcelable.Creator<Person> CREATOR =new Parcelable.Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
//注意读取变量和写入变量的顺序应该一致 不然得不到正确的结果
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(sex);
}
//注意读取变量和写入变量的顺序应该一致 不然得不到正确的结果
public void readFromParcel(Parcel source) {
name = source.readString();
sex = source.readInt();
}
//////////////////////////////////////////////////////////////////
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
}
2.定义远程服务接口和服务组件
IGreetService.aidl文件:
package com.liujun.aidl;
import com.liujun.aidl.Person;
interface IGreetService{
String greet(in Person person);
}
AIDLService.java文件:
package com.liujun.service;
import com.liujun.aidl.IGreetService;
import com.liujun.aidl.Person;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class AIDLService extends Service {
private static final String TAG = "liujun";
@Override
public void onCreate() {
Log.i(TAG, "onCreate() called");
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "onBind() called");
return stub;
}
//服务接口实例对象
IGreetService.Stub stub=new IGreetService.Stub() {
@Override
public String greet(Person person) throws RemoteException {
Log.i(TAG, "greet(Person person) called");
return ServiceMethod(person);
}
};
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind() called");
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy() called");
}
/////////////////-----------------------------------
/**
* 服务组件方法
* @param person
* @return
*/
public String ServiceMethod(Person person){
String greeting = "hello, " + person.getName();
switch (person.getSex()) {
case 0:
greeting = greeting + ", you‘re handsome."; 工程代码
break;
case 1:
greeting = greeting + ", you‘re beautiful.";
break;
}
return greeting;
}
}
注册服务:
<!-- 配置服务组件 -->
<service android:name="com.liujun.service.AIDLService">
<intent-filter>
<action android:name="android.intent.action.AIDLService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
3.客户端程序复制服务端程序数据传输对象和接口文件
4.绑定远程服务,调用远程服务方法,传输复杂对象
package com.liujun.parcelableclient; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.example.parcelableclient.R; import com.liujun.aidl.IGreetService; import com.liujun.aidl.Person; public class MainActivity extends Activity { //控件 private Button bindBtn; private Button greetBtn; private Button unbindBtn; private boolean mBound=false;//是否绑定远程服务 //远程服务接口对象 private IGreetService iService; //获取远程服务接口对象 private ServiceConnection conn=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iService=IGreetService.Stub.asInterface(service); mBound=true; } @Override public void onServiceDisconnected(ComponentName name) { mBound=false; iService=null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindBtn=(Button) this.findViewById(R.id.bindBtn); greetBtn=(Button) this.findViewById(R.id.greetBtn); unbindBtn=(Button) this.findViewById(R.id.unbandBtn); //注册点击监听器 MyListener listener=new MyListener(); bindBtn.setOnClickListener(listener); greetBtn.setOnClickListener(listener); unbindBtn.setOnClickListener(listener); } /** * 事件处理器 * @author asus * */ private class MyListener implements OnClickListener{ @Override public void onClick(View v) { switch (v.getId()) { case R.id.bindBtn: //绑定远程服务 Intent intent = new Intent("android.intent.action.AIDLService"); bindService(intent, conn, Context.BIND_AUTO_CREATE); //设置按钮状态 bindBtn.setEnabled(false); greetBtn.setEnabled(true); unbindBtn.setEnabled(true); break; case R.id.greetBtn: try { Person person = new Person(); person.setName("liujun"); person.setSex(0); String retVal = iService.greet(person); Toast.makeText(MainActivity.this, retVal, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); } break; case R.id.unbandBtn: unbindService(conn); bindBtn.setEnabled(true); greetBtn.setEnabled(false); unbindBtn.setEnabled(false); break; } } } @Override protected void onDestroy() { super.onDestroy(); if (mBound) { unbindService(conn); iService=null; } } }工程代码http://download.csdn.net/detail/u010739551/7710977