(转自)可以参见:
1、为什么要有AIDL?
无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在肯定合理,但是你还是没有明白。对于AIDL有一些人的浅显概念就是,AIDL可以跨进程访问其他应用程序,和其他应用程序通讯,那我告诉你,很多技术都可以访问,如广播(应用A在Manifest.xml中注册指定Action的广播)应用B发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完成了通讯(但是这种通讯是单向的);还如ContentProvider,通过URI接口暴露数据给其他应用访问;但是这种都算不上是应用之间的通讯。可能最让人迷惑的是Android推出来了Messager,它就是完成应用之间的通讯的。那么为什么还要有AIDL呢,官方文档介绍AIDL中有这么一句话:
Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
第一句最重要,“只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理。还是官方文档说的明白,一句话就可以理解为什么要有AIDL。那么是不是这样的写个AIDL试试。
2、AIDL使用
- 定义AIDL文件
1 // IRemoteService.aidl 2 package com.example.android; 3 4 // Declare any non-default types here with import statements 5 6 /** Example service interface */ 7 interface IRemoteService { 8 /** Request the process ID of this service, to do evil things with it. */ 9 int getPid();10 11 /** Demonstrates some basic types that you can use as parameters12 * and return values in AIDL.13 */14 void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,15 double aDouble, String aString);16 }
这段代码也是官方文档的。命名为IRemoteService.aidl,放在com.example.android包下(这个可以随意),保存后Android编译器会在gen目录下自动生成IRemoteService.java文件
-
定义我们的服务,DDService.java,并且需要在AndroidManifest.xml中注册,并添加“duanqing.test.aidl” 的ACTION。这样我们的服务端就完成了,把服务端运行到模拟器(或者手机上),等一会可以看一下打印信息,重点看“线程名”
1 package com.example.service; 2 3 import com.example.android.IRemoteService; 4 5 import android.app.Service; 6 import android.content.Intent; 7 import android.os.IBinder; 8 import android.os.Process; 9 10 public class DDService extends Service {11 @Override12 public void onCreate() {13 super.onCreate();14 System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName());15 }16 @Override17 public IBinder onBind(Intent arg0) {18 System.out.println("DDService onBind");19 return mBinder;20 }21 22 private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {23 public int getPid(){24 System.out.println("Thread: " + Thread.currentThread().getName());25 System.out.println("DDService getPid ");26 return Process.myPid();27 }28 public void basicTypes(int anInt, long aLong, boolean aBoolean,29 float aFloat, double aDouble, String aString) {30 System.out.println("Thread: " + Thread.currentThread().getName());31 System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString);32 }33 };34 35 }
- 实现客户端测试代码 新建另一个工程,同样需要添加AIDL协议文件(这是一个标准的协议文件,定义对外服务),这里我列出来我的测试代码
1 package com.example.aidlclient; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.ServiceConnection; 8 import android.os.Bundle; 9 import android.os.IBinder;10 import android.os.Process;11 import android.os.RemoteException;12 import android.view.View;13 14 import com.example.android.IRemoteService;15 16 public class MainActivity extends Activity {17 private IRemoteService remoteService;18 @Override19 public void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.activity_main);22 }23 24 ServiceConnection conn = new ServiceConnection() {25 26 @Override27 public void onServiceDisconnected(ComponentName name) {28 }29 30 @Override31 public void onServiceConnected(ComponentName name, IBinder service) {32 remoteService = IRemoteService.Stub.asInterface(service);33 try {34 int pid = remoteService.getPid();35 int currentPid = Process.myPid();36 System.out.println("currentPID: " + currentPid +" remotePID: " + pid);37 remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白");38 } catch (RemoteException e) {39 e.printStackTrace();40 }41 System.out.println("bind success! " + remoteService.toString());42 }43 };44 45 /**46 * 监听按钮点击47 * @param view48 */49 public void buttonClick(View view) {50 System.out.println("begin bindService");51 Intent intent = new Intent("duanqing.test.aidl");52 bindService(intent, conn, Context.BIND_AUTO_CREATE);53 }54 55 @Override56 protected void onDestroy() {57 super.onDestroy();58 unbindService(conn);59 }60 }
- 执行 点击客户端按钮,执行,看打印信息:看服务端打印,DDService onCreate..........Thread: main,主线程,当客户端调用服务端getPid方法时,服务端是在Thread: Binder2中执行,当客户端调用服务端basicType方法时,服务端是在Thread:Binder1中执行
4、