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;
	}
}

源碼下載


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章