频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
【Android基础知识】AIDL跨进程调用
2017-04-21 09:25:00         来源:自由世界的专栏  
收藏   我要投稿

Android基础知识】AIDL跨进程调用,AIDL(Android interface definition language)Android接口定义语言。

Android 系统中,各应用程序都运行在自己的进程中,进程之间一般无法直接进行数据交换,为了实现这种跨进程通信,Android提供了AIDL Service.

客户端访问Service时,Android并不是直接返回Service对象给客户端,只是将Service的代理(IBinder对象)通过onBind方法返回给客户端。因此Android的AIDL的远程接口的实现类就是那个IBinder实现类。

和绑定本地Service不同的是,本地会把IBinder对象本身返回给onServiceConnected方法的第二个参数。但是远程Service只是将IBinder对象的代理传给客户端ServiceConnected的第二个参数。当客户端获取了远程Service的IBinder对象的代理之后,接下来就可以通过IBinder对象去回调远程Service的属性或方法了。

AIDL使用实例:实现服务器端启动一个服务,随机更改颜色和体重,客户端获取这些信息

服务器端

1.建立aidl文件

package com.aidl;
interface ICat
{
	String getColor();
	double getWeight();
}

2.android会自动生成一个类,类里有stub静态内部类实现了IBinder和Aidl接口。

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\AIDLService\\src\\com\\aidl\\ICat.aidl
 */
package com.aidl;
public interface ICat extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.aidl.ICat
{
private static final java.lang.String DESCRIPTOR = "com.aidl.ICat";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.aidl.ICat interface,
 * generating a proxy if needed.
 */
public static com.aidl.ICat asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.aidl.ICat))) {
return ((com.aidl.ICat)iin);
}
return new com.aidl.ICat.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getColor:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getColor();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getWeight:
{
data.enforceInterface(DESCRIPTOR);
double _result = this.getWeight();
reply.writeNoException();
reply.writeDouble(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.aidl.ICat
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.lang.String getColor() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getColor, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public double getWeight() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
double _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getWeight, _data, _reply, 0);
_reply.readException();
_result = _reply.readDouble();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getColor = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getWeight = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public java.lang.String getColor() throws android.os.RemoteException;
public double getWeight() throws android.os.RemoteException;
}

3.创建我们自己的Service,实现stub类,返回IBinder对象。

/*
 * 定义到AIDL接口之后,需要定义一个Service实现类,该Service的onBind方法
 * 所返回的IBinder对象时ADT所生成的ICat.stub的子类的实例。
 */
public class AidlService extends Service{

	private CatBinder catBinder;
	Timer timer = new Timer();
	String[] colors = new String[]{
			"红色",
			"黄色",
			"黑色"
	};
	double [] weights = new double[]{
			2.3,
			3.1,
			1.58
	};
	private String color;
	private double weight;
	//继承Stub,也就是实现了ICat接口,并实现了IBinder接口
	public class CatBinder extends Stub{

		@Override
		public String getColor() throws RemoteException {
			// TODO Auto-generated method stub
			return color;
		}
		@Override
		public double getWeight() throws RemoteException {
			// TODO Auto-generated method stub
			return weight;
		}	
	}
	
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		catBinder = new CatBinder();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//随机的改变Service组件内color、weight的属性的值
				int rand = (int)(Math.random()*3);
				color = colors[rand];
				weight = weights[rand];
			}
		}, 0,800);
	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		timer.cancel();
	}


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		/*
		 * 返回catBinder对象,绑定本地,直接传递对象
		 * 绑定远程,传递代理。
		 */
		return catBinder;
	}

}

4.创建客户端程序,创建aidl接口文件,注意文件名和包名要完全相同,同样自动生成一个类。

5.绑定service,获取代理。

public class MainActivity extends Activity {

	private ICat catService;
	private Button get;
	EditText color,weight;
	
	private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			catService = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			//获取远程Service的onBind方法返回的对象的代理
			catService = ICat.Stub.asInterface(service);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		get = (Button)findViewById(R.id.button1);
		color = (EditText)findViewById(R.id.editText1);
		weight = (EditText)findViewById(R.id.editText2);
		
		//创建所需绑定的Service的intent
		Intent intent = new Intent();
		intent.setAction("com.aidl.AIDL_Service"); 
		Intent eintent = new Intent(getExplicitIntent(getBaseContext(),intent));
		//绑定远程Service
		bindService(eintent, connection, Service.BIND_AUTO_CREATE);
		
		get.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					color.setText(catService.getColor());
					weight.setText(catService.getWeight()+"");
				}catch(RemoteException e){
					e.printStackTrace();
				}
			}
		});
	}
	//将隐式intent装换为显示intent
    public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
            // Retrieve all services that can match the given intent
            PackageManager pm = context.getPackageManager();
            List resolveInfo = pm.queryIntentServices(implicitIntent, 0);
            // Make sure only one match was found
            if (resolveInfo == null || resolveInfo.size() != 1) {
                return null;
            }
            // Get component info and create ComponentName
            ResolveInfo serviceInfo = resolveInfo.get(0);
            String packageName = serviceInfo.serviceInfo.packageName;
            String className = serviceInfo.serviceInfo.name;
            ComponentName component = new ComponentName(packageName, className);
            // Create a new intent. Use the old one for extras and such reuse
            Intent explicitIntent = new Intent(implicitIntent);
            // Set the component to be explicit
            explicitIntent.setComponent(component);
            return explicitIntent;
        }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.unbindService(connection);
	}	
}

AIDL默认支持的数据类型

基本数据类型(除short)

String、CharSequence、List、Map、Parcelable(序列化)

Android5.0 以后Service不能隐式启动,service Intent must be explitict
源码中是这样的 (源码位置:sdk/sources/android-21/android/app/ContextImpl.java)
private void validateServiceIntent(Intent service) {
            if (service.getComponent() == null && service.getPackage() == null) {
                if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
                    IllegalArgumentException ex = new IllegalArgumentException(
                            "Service Intent must be explicit: " + service);
                    throw ex;
                } else {
                    Log.w(TAG, "Implicit intents with startService are not safe: " + service
                            + " " + Debug.getCallers(2, 3));
                }
            }
        }

有两种解决方式:

1.设置package和packagename

Intent mIntent = new Intent();
    mIntent.setAction("XXX.XXX.XXX");//你定义的service的action
    mIntent.setPackage(getPackageName());//这里你需要设置你应用的包名
    context.startService(mIntent);
2.将隐式转换为显式启动
public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
            // Retrieve all services that can match the given intent
            PackageManager pm = context.getPackageManager();
            List resolveInfo = pm.queryIntentServices(implicitIntent, 0);
            // Make sure only one match was found
            if (resolveInfo == null || resolveInfo.size() != 1) {
                return null;
            }
            // Get component info and create ComponentName
            ResolveInfo serviceInfo = resolveInfo.get(0);
            String packageName = serviceInfo.serviceInfo.packageName;
            String className = serviceInfo.serviceInfo.name;
            ComponentName component = new ComponentName(packageName, className);
            // Create a new intent. Use the old one for extras and such reuse
            Intent explicitIntent = new Intent(implicitIntent);
            // Set the component to be explicit
            explicitIntent.setComponent(component);
            return explicitIntent;
}
Aidl的底层实现原理

\



1.首先Servier去实现了Stub(Binder)内部类,实现了里面的方法并返回一个IBinder对象。

2.客户端调用Stub.asInterface(Binder),这里其实返回的是代理类Proxy对象 return new com.aidl.ICat.Stub.Proxy(obj),该方法代码如下:

public static com.aidl.ICat asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.aidl.ICat))) {
return ((com.aidl.ICat)iin);
}
return new com.aidl.ICat.Stub.Proxy(obj);
}
Proxy(IBinder代理类代码如下)里面实现了我们接口中定义的两个方法,getColor和getWeight
private static class Proxy implements com.aidl.ICat
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.lang.String getColor() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getColor, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public double getWeight() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
double _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getWeight, _data, _reply, 0);
_reply.readException();
_result = _reply.readDouble();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getColor = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getWeight = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
3.客户端利用代理对象Proxy 调用了getColor()方法,Proxy类实现的getColor()方法,该方法会把我们调用的方法的ID Stub.TRANSACTION_getColor 传递给底层IPC
mRemote.transact(Stub.TRANSACTION_getColor, _data, _reply, 0); 
4.到达服务器端调用onTransact方法,该方法会根据刚才传入的方法ID通过switch case 去匹配到TRANSACTION_getColor ,然后会调用 this.getColor()方法,这里的this就
是Stub类对象,我们的Service中 继承了Stub类并且实现了里面的方法,这里方法会被回调this.getColor(),把结果发送给客户端。

public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getColor:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getColor();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getWeight:
{
data.enforceInterface(DESCRIPTOR);
double _result = this.getWeight();
reply.writeNoException();
reply.writeDouble(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}

点击复制链接 与好友分享!回本站首页
上一篇:Android自定义视频播放器(网络/本地)
下一篇:Android仿天猫下拉刷新自定义控件
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站