AIDL的使用
实例:
有一个打印服务需要进行进程间通信,
1.在本地写一个aidl文件接口
2.启动远程service服务,bindService中在ServiceConnection得到打印服务,
3.调用远程服务aidl方法
如果对Android比较熟悉,那么一定使用过AIDL,如果你还不了解,那么也没关系,下面会使用一个例子展示AIDL的用法。
我们使用AIDL实现一个跨进程的加减法调用
1、服务端
新建一个项目,创建一个包名:com.zhy.calc.aidl,在包内创建一个ICalcAIDL文件:
- package com.zhy.calc.aidl;
- interface ICalcAIDL
- {
- int add(int x , int y);
- int min(int x , int y );
- }
注意,文件名为ICalcAIDL.aidl
然后在项目的gen目录下会生成一个ICalcAIDL.java文件,暂时不贴这个文件的代码了,后面会详细说明
然后我们在项目中新建一个Service,代码如下:
- package com.example.zhy_binder;
-
- import com.zhy.calc.aidl.ICalcAIDL;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
-
- public class CalcService extends Service
- {
- private static final String TAG = "server";
-
- public IBinder onBind(Intent t)
- {
- Log.e(TAG, "onBind");
- return mBinder;
- }
-
- private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub()
- {
-
- @Override
- public int add(int x, int y) throws RemoteException
- {
- return x + y;
- }
-
- @Override
- public int min(int x, int y) throws RemoteException
- {
- return x - y;
- }
-
- };
-
- }
在此Service中,使用生成的ICalcAIDL创建了一个mBinder的对象,并在Service的onBind方法中返回
最后记得在AndroidManifest中注册
- <service android:name="com.example.zhy_binder.CalcService" >
- <intent-filter>
- <action android:name="com.zhy.aidl.calc" />
-
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </service>
这里我们指定了一个name,因为我们一会会在别的应用程序中通过Intent来查找此Service;这个不需要Activity,所以我也就没写Activity,安装完成也看不到安装图标,悄悄在后台运行着。
到此,服务端编写完毕。下面开始编写客户端
2、客户端
客户端的代码比较简单,创建一个布局,里面包含4个按钮,分别为绑定服务,解除绑定,调用加法,调用减法
布局文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
-
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="bindService"
- android:text="BindService" />
-
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="unbindService"
- android:text="UnbindService" />
-
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="addInvoked"
- android:text="12+12" />
-
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="minInvoked"
- android:text="50-12" />
-
- </LinearLayout>
主Activity
- package com.example.zhy_binder_client;
-
- 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.util.Log;
- import android.view.View;
- import android.widget.Toast;
-
- import com.zhy.calc.aidl.ICalcAIDL;
-
- public class MainActivity extends Activity
- {
- private ICalcAIDL mCalcAidl;
-
- private ServiceConnection mServiceConn = new ServiceConnection()
- {
- @Override
- public void onServiceDisconnected(ComponentName name)
- {
- Log.e("client", "onServiceDisconnected");
- mCalcAidl = null;
- }
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- Log.e("client", "onServiceConnected");
- mCalcAidl = ICalcAIDL.Stub.asInterface(service);
- }
- };
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- }
-
-
-
-
-
- public void bindService(View view)
- {
- Intent intent = new Intent();
- intent.setAction("com.zhy.aidl.calc");
- bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
- }
-
-
-
-
- public void unbindService(View view)
- {
- unbindService(mServiceConn);
- }
-
-
-
-
- public void addInvoked(View view) throws Exception
- {
-
- if (mCalcAidl != null)
- {
- int addRes = mCalcAidl.add(12, 12);
- Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();
- } else
- {
- Toast.makeText(this, "服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)
- .show();
-
- }
-
- }
-
-
-
-
- public void minInvoked(View view) throws Exception
- {
-
- if (mCalcAidl != null)
- {
- int addRes = mCalcAidl.min(58, 12);
- Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();
- } else
- {
- Toast.makeText(this, "服务端未绑定或被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)
- .show();
-
- }
-
- }
-
- }
很标准的绑定服务的代码。
直接看运行结果:
我们首先点击BindService按钮,查看log
- 08-09 22:56:38.959: E/server(29692): onCreate
- 08-09 22:56:38.959: E/server(29692): onBind
- 08-09 22:56:38.959: E/client(29477): onServiceConnected
可以看到,点击BindService之后,服务端执行了onCreate和onBind的方法,并且客户端执行了onServiceConnected方法,标明服务器与客户端已经联通
然后点击12+12,50-12可以成功的调用服务端的代码并返回正确的结果
下面我们再点击unBindService
- 08-09 22:59:25.567: E/server(29692): onUnbind
- 08-09 22:59:25.567: E/server(29692): onDestroy
由于我们当前只有一个客户端绑定了此Service,所以Service调用了onUnbind和onDestory
然后我们继续点击12+12,50-12,通过上图可以看到,依然可以正确执行,也就是说即使onUnbind被调用,连接也是不会断开的,那么什么时候会端口呢?
即当服务端被异常终止的时候,比如我们现在在手机的正在执行的程序中找到该服务:
似乎和Binder框架没什么关系,下面我们来具体看一看AIDL为什么做了些什么。