Android AIDL通信之对象与普通数据

左图为客户端,右图为服务端:

客户端:

1.建立个aidl文件,IRemoteService.aidl:

package aidlserver;
import aidlserver.Student;
interface IRemoteService{
	String MyString(String a);
	Student GetObject(inout Student stu);
}
2.创建需要传输的对象,比如Student

注意:对象传输需要实现序列化,具体详见:Android常用对象的2种序列化

Student.java:

package aidlserver;

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

public class Student implements Parcelable{

	private String name;
	private int age;
	public Student() {}
	
	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;
	}

	public static final Creator<Student> CREATOR = new Creator<Student>() {
		@Override
		public Student createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			Student mStudent = new Student();
			mStudent.name = source.readString();
			mStudent.age = source.readInt();
			return mStudent;
		}
		@Override
		public Student[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Student[size];
		}
	};
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(name);
		dest.writeInt(age);
	}

	public void readFromParcel(Parcel _reply) {
		this.name = _reply.readString();
		this.age = _reply.readInt();
	}
}
还需要建一个Student.aidl:

package aidlserver;
parcelable Student;
注意:客户端与服务端的aidlserver包里面内容一样,复制黏贴。

MainActivity.java:

package com.example.aidlclient;

import aidlserver.IRemoteService;
import aidlserver.Student;
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.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TextView text;
	private boolean isConn = false;
	protected ServiceConnection conn;
	private IRemoteService mIRemoteService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (TextView) findViewById(R.id.text);
		conn = new ServiceConnection() {
			@Override
			public void onServiceDisconnected(ComponentName name) {Log.d("TAG", "onServiceDisconnected");}
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				// TODO Auto-generated method stub
				Log.d("TAG", "onServiceConnected");
				System.out.println("onServiceConnected");
				isConn = true;
				mIRemoteService = IRemoteService.Stub.asInterface(service);
			}
		};
		
		
		
		new Thread(new Runnable() {
			public void run() {
				bindService(new Intent("com.MYSERVICE"), conn, Context.BIND_AUTO_CREATE);
			}
		}).start();
		
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isConn && mIRemoteService!=null){
					try {
						Student stu = new Student();
						stu.setName("a");
						stu.setAge(10);
						Student mStudent = mIRemoteService.GetObject(stu);
						StringBuffer sb = new StringBuffer();
						sb.append("字符:"+mIRemoteService.MyString("1"));
						sb.append("名字:"+mStudent.getName()+"年龄:"+mStudent.getAge());
						text.setText(sb);
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unbindService(conn);
		super.onDestroy();
	}
}
AIDLService.java:

package aidlservice;

import aidlserver.IRemoteService;
import aidlserver.Student;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

public class AIDLService extends Service {

	private IRemoteService.Stub mBinder = new IRemoteService.Stub() {
		
		@Override
		public String MyString(String a) throws RemoteException {
			// TODO Auto-generated method stub
			return "110";
		}

		@Override
		public Student GetObject(Student stu) throws RemoteException {
			// TODO Auto-generated method stub
			stu.setName("1111");
			stu.setAge(1111);
			return stu;
		}
	};
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onBind");
		return mBinder;
	}
}

源码下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值