上一篇文章中簡單的寫了一下關於Android中Service的兩種啟動方式,不過都是本地的服務,今天就簡單的寫下關於Android中遠程Service的使用,學習之前先了解兩個概念,AIDL( Android Interface definition language)字面上的意思就是借口定義語言,專業一點理解就是Android進程之間通信的接口描述語言。IPC(Inter-Process Conmmunication)內部進程之間的通信,同一個手機上,如果你的APP需要訪問調用另外一個APP的服務,通信的方式就是IPC。
同一個APP中Service調用
跟上篇文章不同,這次先自行創建一個名稱為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>
調用結果如下:
不同的App之間的調用
不同之間的調用,由於相互之間要相互通信,同樣的需要定義與服務端的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文件。大同小異,各位可以自行研究,一不小心又周五了,哎~