上一篇文章中简单的写了一下关于Android中Service的两种启动方式,不过都是本地的服务,今天就简单的写下关于Android中远程Service的使用,学习之前先了解两个概念,AIDL( Android Interface definition language)字面上的意思就是借口定义语言,专业一点理解就是Android进程之间通信的接口描述语言。IPC(Inter-Process Conmmunication)内部进程之间的通信,同一个手机上,如果你的APP需要访问调用另外一个APP的服务,通信的方式就是IPC。
跟上篇文章不同,这次先自行创建一个名称为BookAIDLService.aidl的AIDL文件:
package com.remote.service; interface BookAIDLService { int sum(int a,int b); }
吐槽一下,网上很多都是这么写的,自己新增的时候没有找到如何新建一个AIDL文件,你首先需要建一个BookAIDLService.java文件,然后修改后缀名为aidl,这个时候看到效果如下:
保存之后,会自动的在gen目录下生成一个BookAIDLService.java文件,还是跟最开始一样,看下应用程序页面:
本地事件针对的是第三个按钮,先来重写下BookService:
public class BookService extends Service { private String tag = "BookService"; BookAIDLService.Stub bookAIDLBinderStub=new Stub() { @Override public int sum(int a, int b) throws RemoteException { // TODO Auto-generated method stub return a+b; } }; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.i(tag, "开始onCreate启动了"); Log.i("BookService","BookService的ID:"+Process.myPid()); // try { // Thread.sleep(40000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.i(tag, "开始执行onStartCommand启动了"); Toast.makeText(this, "BookService开始了", Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub Log.i(tag, "销毁onDestroy启动了"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i(tag, "绑定onBind启动了"); return bookAIDLBinderStub; } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i(tag, "解绑onUnbind启动了"); return super.onUnbind(intent); } class BookBinder extends Binder { public BookService getCurrentService() { return BookService.this; } } }
跟之前最大的不同就是在onBind方法中返回一个bookAIDLBinderStub,同时上次写的BookConnection也要冲洗写一下:
class BookServiceConnection implements ServiceConnection{ private BookAIDLService bookAIDLService; public BookServiceConnection() { super(); // TODO Auto-generated constructor stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub //获取实例 //BookService bookService=((BookService.BookBinder)service).getCurrentService(); //just do wo you want to do BookAIDLService bookAIDLService=BookAIDLService.Stub.asInterface(service); try { int result=bookAIDLService.sum(10, 100); Log.i("BookService", "BookAIDLService调用结果:"+result); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } } }
前台的调用是:
Intent binderStartIntent=new Intent("com.example.googleservice.BookService.AIDL"); connection=new BookServiceConnection(); bindService(binderStartIntent, connection,Context.BIND_AUTO_CREATE);
这里Intent是隐式调用,如果不是很熟悉可以参考我之前的文章,AndroidManifest.xml文件中需要重新改动一下:
<service android:name="com.example.googleservice.BookService" android:process=":remote"> <intent-filter> <action android:name="com.example.googleservice.BookService.AIDL"/> </intent-filter> </service>
调用结果如下:
不同之间的调用,由于相互之间要相互通信,同样的需要定义与服务端的aidl名相同的aidl,新建一个Android项目,然后结构如下:
客户端页面就不用写了,就一个调用按钮,客户端把服务端的BookConnection拷贝过来:
class BookServiceConnection implements ServiceConnection{ private BookAIDLService bookAIDLService; public BookServiceConnection() { super(); // TODO Auto-generated constructor stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub //获取实例 //BookService bookService=((BookService.BookBinder)service).getCurrentService(); //just do wo you want to do BookAIDLService bookAIDLService=BookAIDLService.Stub.asInterface(service); try { int result=bookAIDLService.sum(10, 100); Log.i("BookService", "客户端BookAIDLService调用结果:"+result); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } }
客户端调用:
Intent binderStartIntent=new Intent("com.example.googleservice.BookService.AIDL"); connection=new BookServiceConnection(); bindService(binderStartIntent, connection,Context.BIND_AUTO_CREATE);
调用结果如下:
好了,至此简单的讲了一下Android中的远程服务调用,很多概念没有讲,不会掉书袋,有兴趣可以自己私下了解下,不同的进程之间传递数据,Android对这类数据的格式支持是非常有限,基本上只能传递Java的基本数据类型、字符串、List或Map,如果想传一个自定义的类,必须要让这个类去实现Parcelable接口,并且要给这个类也定义一个同名的AIDL文件。大同小异,各位可以自行研究,一不小心又周五了,哎~