Android中AIDL细解及实例

一、基本概念
        aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
        icp:interprocess communication :内部进程通信。
        在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的。因此要传递对象, 需要把对象解析成操作系统能够理解的数据格式, 以达到跨界对象访问的目的。在JavaEE中,采用RMI通过序列化传递对象。在Android中, 则采用AIDL(Android Interface Definition Language:接口定义语言)方式实现。
        (1)在Eclipse Android工程的Java包目录中建立一个扩展名为aidl的文件。该文件的语法类似于Java代码,但会稍有不同。
        (2)如果aidl文件的内容是正确的,ADT会自动生成一个Java接口文件(*.java)。
        (3)建立一个服务类(Service的子类)。
        (4)实现由aidl文件生成的Java接口。
        (5)在AndroidManifest.xml文件中配置AIDL服务,尤其要注意的是,<action>标签中android:name的属性值就是客户端要引用该服务的ID,也就是Intent类的参数值。 

         AIDL是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现Android设备上的两个进程间通信(IPC)。AIDL的IPC机制和EJB所采用的CORBA很类似,进程之间的通信信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。

二、实例

        1)本实例分为客户端和服务端两部分,分别为客户端和服务端新建一个eclipse工程,实现了从客户端向服务端发送请求,服务端打印log的功能。

        客户端和服务端的源码结构如下:

              

       注意,1、由于客户端和服务端的aidl文件所在包名必须一样,而两个包名一样的程序在安装时会产生冲突,所以这里用了一个技巧,在客户端工程的AndroidManifest.xml里把包名指定为com.styleflying,所以大家就会看到gen目录下的R.java所在的包是com.styleflying而不是com.styleflying.AIDL。
                   2、为了便于观察,在服务端添加了一个MainActivity,使对比结果具有可视化效果。

       2)现在客户端和服务端工程分别新建一个aidl接口,所在包和文件名必须一样。两个aidl接口是一样的,内容如下:

package com.styleflying.AIDL;  
interface mInterface{  
    void invokTest();  
} 
自动编译生成.java文件如下:
/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: /home/hwruanjian/workspace1/AidlDemo_service/src/com/styleflying/AIDL/mInterface.aidl
 */
package com.styleflying.AIDL;
public interface mInterface extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.styleflying.AIDL.mInterface
{
private static final java.lang.String DESCRIPTOR = "com.styleflying.AIDL.mInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.styleflying.AIDL.mInterface interface,
 * generating a proxy if needed.
 */
public static com.styleflying.AIDL.mInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.styleflying.AIDL.mInterface))) {
return ((com.styleflying.AIDL.mInterface)iin);
}
return new com.styleflying.AIDL.mInterface.Stub.Proxy(obj);
}
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_invokTest:
{
data.enforceInterface(DESCRIPTOR);
this.invokTest();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.styleflying.AIDL.mInterface
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void invokTest() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_invokTest, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_invokTest = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void invokTest() throws android.os.RemoteException;
}
客户端的mAIDLActivity.java如下:

package com.styleflying.AIDL;

import com.styleflying.R;

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.util.Log;

public class mAIDLActivity extends Activity {
	
    private static final String TAG = "AIDLActivity";  
    private Button btnOk;  
    private Button btnCancel;  
    private Button btnCallBack; 
	
    private void Log(String str){  
        Log.i(TAG,"jptest_----------" + str + "----------");  
    }  
      
          
    mInterface mService;  
    private ServiceConnection mConnection = new ServiceConnection(){  
        public void onServiceConnected(ComponentName className,  
                IBinder service){  
            Log("jptest_connect service");  
            mService = mInterface.Stub.asInterface(service);  
        }  
          
        public void onServiceDisconnected(ComponentName className){  
            Log("jptest_disconnect service");  
            mService = null;  
        }  
    };  
      
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnOk = (Button)findViewById(R.id.btn_ok);  
        btnCancel = (Button)findViewById(R.id.btn_cancel);  
        btnCallBack = (Button)findViewById(R.id.btn_callback);
        
        btnOk.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
            	Log("jptest_btnOk.setOnClickListener");
                Bundle args = new Bundle();  
                Intent intent = new Intent("com.styleflying.AIDL.service");  
                intent.putExtras(args);  
                bindService(intent,mConnection,Context.BIND_AUTO_CREATE);  
            }  
        });  
          
        btnCancel.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
            	Log("jptest_btnCancel.setOnClickListener");
                unbindService(mConnection);  
            }  
        });       
        
        btnCallBack.setOnClickListener(new OnClickListener(){  
            public void onClick(View v){  
                try{  
                    Log.i(TAG,"jptest_current Thread id = " + Thread.currentThread().getId());  
                    mService.invokTest();  
                }  
                catch(RemoteException e){  
                      
                }  
            }  
        });  
        
    }
}

        客户端在执行bindService的时候,成功绑定服务之后,会回调mConnection的onServiceConnected(),并且传回了服务端的通信接口IBinder,此IBinder即服务onBind()时返回的IBinder,详见mAIDLService.java。

        在onServiceConnected(),客户端成功获取了服务端通信接口,实际上是本地代理对象,该对象存在于客户端进程空间,客户端只和代理对象交互,真正的IPC通信是本地代理对象和服务端的通信。

服务端的mAIDLService.java如下:

package com.styleflying.AIDL;  

import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.os.Looper;  
import android.os.RemoteException;  
import android.util.Log;  
import android.widget.Toast;  
public class mAIDLService extends Service{  
    private static final String TAG = "AIDLService";  
      
    private void Log(String str){  
        Log.i(TAG,"jptest_----------" + str + "----------");  
    }  
      
    public void onCreate(){  
        Log("jptest_service created");  
    }  
      
    public void onStart(Intent intent, int startId){  
        Log("jptest_service started id = " + startId);  
    }  
      
    public IBinder onBind(Intent t){  
        Log("jptest_service on bind");  
        return mBinder;  
    }  
      
    public void onDestroy(){  
        Log("jptest_service on destroy");  
        super.onDestroy();  
    }  
      
    public boolean onUnbind(Intent intent){  
        Log("jptest_service on unbind");  
        return super.onUnbind(intent);  
    }  
      
    public void onRebind(Intent intent){  
        Log("jptest_service on rebind");  
        super.onRebind(intent);  
    }  
      
      
    private final mInterface.Stub mBinder = new mInterface.Stub() {       
        public void invokTest() throws RemoteException {  
            // TODO Auto-generated method stub  
            Log.e(TAG, "jptest_remote call from client! current thread id = " + Thread.currentThread().getId());  
        }  
    };  
}  
        注意onBind()函数,返回了mBinder,而mBinder实现了mInterface.Stub,实现了mInterface接口,执行了打印log的操作。

        由于客户端只和本地代理对象即服务句柄通信,由代理对象进行真正的IPC操作,所以对客户端来说,IPC过程是透明的,调用远程操作如同调用本地操作一样。在客户端调用transact()时,会将服务描述DSCRIPTION写入到data里,在客户端onTransact时会验证,如果两个不一样,则不能通信。而DSCRIPTION是根据mInterface包名和接口名自动生成的,这就是为什么两个工程里的mInterface.aidl要在同一个包的原因。
        在这个过程中,mInterface.aidl起到了桥梁的作用,规定统一了客户端和服务端的通信接口,使得客户端和服务端得以成功的通信。
        具体的通信transact和onTransact的过程也就是利用Binder驱动通信的过程,在这里就不多叙述。

服务端的MainActivity.java如下:

package com.styleflying.AIDL;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
客户端的AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.styleflying"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AIDL.mAIDLActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
服务端的AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.styleflying.AIDL"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".mAIDLService">  
                <intent-filter>  
                <action android:name="com.styleflying.AIDL.service" />  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </service>  
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


推荐阅读:http://blog.csdn.net/luoshengyang/article/details/6618363

源码地址如下:http://download.csdn.net/detail/jiulousanti/7291861


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值