Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> android進程間通信:使用AIDL(1)

android進程間通信:使用AIDL(1)

編輯:初級開發

歡迎閱讀本文,你能關注本文,你知道你需要進程間通信、需要AIDL(以及Binder),那麼可以默認你對這些概念已經有了一些了解,你(大致)知道它們是什麼,它們有什麼用,所以為了節約大家的眼力和時間,在此我不復制粘貼網上泛濫的博客或者翻譯冗長的android文檔。

      關於AIDL的介紹在文檔:docs/guide/developing/tools/aidl.Html

      關於IBinder的介紹在文檔:docs/reference/android/os/IBinder.Html

      以及Binder:docs/reference/android/os/Binder.Html

      在後文中,我將以我自己的理解向你介紹相關的概念。以我目前粗淺的經驗,應用程序使用AIDL的地方,幾乎都和Service有關,所以你也需要知道一些關於Service的知識。日後得閒我也會繼續寫一些關於Service的貼。

      本文將以一個例子來和你分享使用AIDL的基礎技能,這個例子裡有:

1、一個類mAIDLActivity,繼承Activity。裡面有三個按鈕,text分別為StartService,StopService,CallbackTest。

2、一個類mAIDLService,繼承Service。為了充分展示ADIL的功能,它做以下工作:當用戶點擊CallbackTest按鈕時,從mAIDLActivity調用mAIDLService中的Stub 對象的一個方法invokCallBack(),而這個方法又會調用mAIDLActivity中Stub 對象的一個方法perforMaction(),這個方法在屏幕上顯示一個toast。沒什麼意義,只是展示一下AIDL如何使用。

3、兩個AIDL文件:forService.aidl和forActivity.aidl。對應名字,在Service和Activity中分別有對象需要用到它們定義的接口。

4、相關XML文件,略過。關於manifest中Service的語法,見docs/guide/topics/manifest /service-element.Html。你也可以簡單地在<application></application>中加入

     <service android:name=".mAIDLService" android:process=":remote"> </service>

開發環境為Eclipse。

揀重要的先說,來看看aidl文件的內容:

文件:forActivity.aidl

vIEw plain copy to clipboard print ? 
01.package  com.styleflying.AIDL;   
02.interface  forActivity {   
03.    void  perforMaction();   
04.    }   
vIEw plaincopy to clipboardprint?
01.package com.styleflying.AIDL;   
02.interface forActivity {   
03.    void perforMaction();   
04.    }  
package com.styleflying.AIDL;
interface forActivity {
 void perforMaction();
 }

  
文件:forService.aidl

vIEw plain copy to clipboard print ? 
01.package  com.styleflying.AIDL;   
02.import  com.styleflying.AIDL.forActivity;   
03.interface  forService {   
04.    void  registerTestCall(forActivity cb);   
05.    void  invokCallBack();   
06.}   
vIEw plaincopy to clipboardprint?
01.package com.styleflying.AIDL;   
02.import com.styleflying.AIDL.forActivity;   
03.interface forService {   
04.    void registerTestCall(forActivity cb);   
05.    void invokCallBack();   
06.}  
package com.styleflying.AIDL;
import com.styleflying.AIDL.forActivity;
interface forService {
 void registerTestCall(forActivity cb);
 void invokCallBack();
}

  
這兩個文件和Java文件放置的地方一樣,看包名。

在Eclipse中它們將被自動編譯為forActivity.java和forService.Java,它們存放在gen目錄下。為了方便手頭無法演練的讀者,代碼貼上,不用細看。

文件forActivity.Java:

vIEw plain copy to clipboard print ? 
01./*   
02. * This file is auto-generated.  DO NOT MODIFY.   
03. * Original file: D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forActivity.aidl  
04. */    
05.package  com.styleflying.AIDL;   
06.import  Java.lang.String;   
07.import  android.os.RemoteException;   
08.import  android.os.IBinder;   
09.import  android.os.IInterface;   
10.import  android.os.Binder;   
11.import  android.os.Parcel;   
12.public   interface  forActivity  extends  android.os.IInterface   
13.{   
14./** Local-side IPC implementation stub class. */    
15.public   static   abstract   class  Stub  extends  android.os.Binder  implements  com.styleflying.AIDL.forActivity   
16.{   
17.private   static   final  Java.lang.String DESCRIPTOR =  "com.styleflying.AIDL.forActivity" ;   
18./** Construct the stub at attach it to the interface. */    
19.public  Stub()   
20.{   
21.this .attachInterface( this , DESCRIPTOR);   
22.}   
23./**   
24. * Cast an IBinder object into an forActivity interface,   
25. * generating a proxy if needed.   
26. */    
27.public   static  com.styleflying.AIDL.forActivity asInterface(android.os.IBinder obj)   
28.{   
29.if  ((obj== null )) {   
30.return   null ;   
31.}   
32.android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);  
33.if  (((iin!= null )&&(iin  instanceof  com.styleflying.AIDL.forActivity))) {   
34.return  ((com.styleflying.AIDL.forActivity)iin);   
35.}   
36.return   new  com.styleflying.AIDL.forActivity.Stub.Proxy(obj);   
37.}   
38.public  android.os.IBinder asBinder()   
39.{   
40.return   this ;   
41.}   
42.@Override   public   boolean  onTransact( int  code, android.os.Parcel data, android.os.Parcel reply,  int  flags)  throws  android.os.RemoteException   
43.{   
44.switch  (code)   
45.{   
46.case  INTERFACE_TRANSACTION:   
47.{   
48.reply.writeString(DESCRIPTOR);   
49.return   true ;   
50.}   
51.case  TRANSACTION_perforMaction:   
52.{   
53.data.enforceInterface(DESCRIPTOR);   
54.this .perforMaction();   
55.reply.writeNoException();   
56.return   true ;   
57.}   
58.}   
59.return   super .onTransact(code, data, reply, flags);   
60.}   
61.private   static   class  Proxy  implements  com.styleflying.AIDL.forActivity   
62.{   
63.private  android.os.IBinder mRemote;   
64.Proxy(android.os.IBinder remote)   
65.{   
66.mRemote = remote;   
67.}   
68.public  android.os.IBinder asBinder()   
69.{   
70.return  mRemote;   
71.}   
72.public  Java.lang.String getInterfaceDescriptor()   
73.{   
74.return  DESCRIPTOR;   
75.}   
76.public   void  perforMaction()  throws  android.os.RemoteException   
77.{   
78.android.os.Parcel _data = android.os.Parcel.obtain();   
79.android.os.Parcel _reply = android.os.Parcel.obtain();   
80.try  {   
81._data.writeInterfaceToken(DESCRIPTOR);   
82.mRemote.transact(Stub.TRANSACTION_perforMaction, _data, _reply, 0 );   
83._reply.readException();   
84.}   
85.finally  {   
86._reply.recycle();   
87._data.recycle();   
88.}   
89.}   
90.}   
91.static   final   int  TRANSACTION_perforMaction = (IBinder.FIRST_CALL_TRANSACTION +  0 );   
92.}   
93.public   void  perforMaction()  throws  android.os.RemoteException;   
94.}   
vIEw plaincopy to clipboardprint?
01./*  
02. * This file is auto-generated.  DO NOT MODIFY.  
03. * Original file:   
04.D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forActivity.aidl 
05. */  
06.package com.styleflying.AIDL;   
07.import Java.lang.String;   
08.import android.os.RemoteException;   
09.import android.os.IBinder;   
10.import android.os.IInterface;   
11.import android.os.Binder;   
12.import android.os.Parcel;   
13.public interface forActivity extends android.os.IInterface   
14.{   
15./** Local-side IPC implementation stub class. */  
16.public static abstract class Stub extends android.os.Binder implements    
17.com.styleflying.AIDL.forActivity   
18.{   
19.private static final Java.lang.String DESCRIPTOR =    
20."com.styleflying.AIDL.forActivity";   
21./** Construct the stub at attach it to the interface. */ 
22.public Stub()   
23.{   
24.this.attachInterface(this, DESCRIPTOR);   
25.}   
26./**  
27. * Cast an IBinder object into an forActivity interface, 
28. * generating a proxy if needed.  
29. */  
30.public static com.styleflying.AIDL.forActivity    
31.asInterface(android.os.IBinder obj)   
32.{   
33.if ((obj==null)) {   
34.return null;   
35.}   
36.android.os.IInterface iin =    
37.(android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);  
38.if (((iin!=null)&&(iin instanceof    
39.com.styleflying.AIDL.forActivity))) {   
40.return ((com.styleflying.AIDL.forActivity)iin);   
41.}   
42.return new com.styleflying.AIDL.forActivity.Stub.Proxy(obj);   
43.}   
44.public android.os.IBinder asBinder()   
45.{   
46.return this;   
47.}   
48.@Override public boolean onTransact(int code, android.os.Parcel data,    
49.android.os.Parcel reply, int flags) throws android.os.RemoteException   
50.{   
51.switch (code)   
52.{   
53.case INTERFACE_TRANSACTION:   
54.{   
55.reply.writeString(DESCRIPTOR);   
56.return true;   
57.}   
58.case TRANSACTION_perforMaction:   
59.{   
60.data.enforceInterface(DESCRIPTOR);   
61.this.perforMaction();   
62.reply.writeNoException();   
63.return true;   
64.}   
65.}   
66.return super.onTransact(code, data, reply, flags);   
67.}   
68.private static class Proxy implements com.styleflying.AIDL.forActivity   
69.{   
70.private android.os.IBinder mRemote;   
71.Proxy(android.os.IBinder remote)   
72.{   
73.mRemote = remote;   
74.}   
75.public android.os.IBinder asBinder()   
76.{   
77.return mRemote;   
78.}   
79.public Java.lang.String getInterfaceDescriptor()   
80.{   
81.return DESCRIPTOR;   
82.}   
83.public void perforMaction() throws android.os.RemoteException   
84.{   
85.android.os.Parcel _data = android.os.Parcel.obtain();   
86.android.os.Parcel _reply = android.os.Parcel.obtain();   
87.try {   
88._data.writeInterfaceToken(DESCRIPTOR);   
89.mRemote.transact(Stub.TRANSACTION_perforMaction, _data, _reply, 0);   
90._reply.readException();   
91.}   
92.finally {   
93._reply.recycle();   
94._data.recycle();   
95.}   
96.}   
97.}   
98.static final int TRANSACTION_perforMaction =    
99.(IBinder.FIRST_CALL_TRANSACTION + 0);   
100.}   
101.public void perforMaction() throws android.os.RemoteException;   
102.}  
/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: 
D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forActivity.aidl
 */
package com.styleflying.AIDL;
import Java.lang.String;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Binder;
import android.os.Parcel;
public interface forActivity extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements 
com.styleflying.AIDL.forActivity
{
private static final Java.lang.String DESCRIPTOR = 
"com.styleflying.AIDL.forActivity";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an forActivity interface,
 * generating a proxy if needed.
 */
public static com.styleflying.AIDL.forActivity 
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.forActivity))) {
return ((com.styleflying.AIDL.forActivity)iin);
}
return new com.styleflying.AIDL.forActivity.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_perforMaction:
{
data.enforceInterface(DESCRIPTOR);
this.perforMaction();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.styleflying.AIDL.forActivity
{
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 perforMaction() 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_perforMaction, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_perforMaction = 
(IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void perforMaction() throws android.os.RemoteException;
}
 

文件forService.Java:

+ expand source vIEw plain copy to clipboard print ? 
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150/*  
 * This file is auto-generated.  DO NOT MODIFY.   
 * Original file: D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forService.aidl  
 */    
package  com.styleflying.AIDL;   
import  Java.lang.String;   
import  android.os.RemoteException;   
import  android.os.IBinder;   
import  android.os.IInterface;   
import  android.os.Binder;   
import  android.os.Parcel;   
public   interface  forService  extends  android.os.IInterface   
{   
/** Local-side IPC implementation stub class. */    
public   static   abstract   class  Stub  extends  android.os.Binder  implements  com.styleflying.AIDL.forService   
{   
private   static   final  Java.lang.String DESCRIPTOR =  "com.styleflying.AIDL.forService" ;   
/** Construct the stub at attach it to the interface. */    
public  Stub()   
{   
this .attachInterface( this , DESCRIPTOR);   
}   
/**   
 * Cast an IBinder object into an forService interface,   
 * generating a proxy if needed.   
 */    
public   static  com.styleflying.AIDL.forService 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.forService))) {   
return  ((com.styleflying.AIDL.forService)iin);   
}   
return   new  com.styleflying.AIDL.forService.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_registerTestCall:   
{   
data.enforceInterface(DESCRIPTOR);   
com.styleflying.AIDL.forActivity _arg0;   
_arg0 = com.styleflying.AIDL.forActivity.Stub.asInterface(data.readStrongBinder());  
this .registerTestCall(_arg0);   
reply.writeNoException();   
return   true ;   
}   
case  TRANSACTION_invokCallBack:   
{   
data.enforceInterface(DESCRIPTOR);   
this .invokCallBack();   
reply.writeNoException();   
return   true ;   
}   
}   
return   super .onTransact(code, data, reply, flags);   
}   
private   static   class  Proxy  implements  com.styleflying.AIDL.forService   
{   
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  registerTestCall(com.styleflying.AIDL.forActivity cb)  throws  android.os.RemoteException   
{   
android.os.Parcel _data = android.os.Parcel.obtain();   
android.os.Parcel _reply = android.os.Parcel.obtain();   
try  {   
_data.writeInterfaceToken(DESCRIPTOR);   
_data.writeStrongBinder((((cb!=null ))?(cb.asBinder()):( null )));   
mRemote.transact(Stub.TRANSACTION_registerTestCall, _data, _reply, 0 );   
_reply.readException();   
}   
finally  {   
_reply.recycle();   
_data.recycle();   
}   
}   
public   void  invokCallBack()  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_invokCallBack, _data, _reply, 0 );   
_reply.readException();   
}   
finally  {   
_reply.recycle();   
_data.recycle();   
}   
}   
}   
static   final   int  TRANSACTION_registerTestCall = (IBinder.FIRST_CALL_TRANSACTION +  0 );   
static   final   int  TRANSACTION_invokCallBack = (IBinder.FIRST_CALL_TRANSACTION +  1 );   
}   
public   void  registerTestCall(com.styleflying.AIDL.forActivity cb)  throws  android.os.RemoteException;   
public   void  invokCallBack()  throws  android.os.RemoteException;   
}   
+ expand sourcevIEw plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150/* 
 * This file is auto-generated.  DO NOT MODIFY.  
 * Original file:   
D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forService.aidl 
 */  
package com.styleflying.AIDL;   
import Java.lang.String;   
import android.os.RemoteException;   
import android.os.IBinder;   
import android.os.IInterface;   
import android.os.Binder;   
import android.os.Parcel;   
public interface forService extends android.os.IInterface   
{   
/** Local-side IPC implementation stub class. */  
public static abstract class Stub extends android.os.Binder implements    
com.styleflying.AIDL.forService   
{   
private static final Java.lang.String DESCRIPTOR =    
"com.styleflying.AIDL.forService";   
/** Construct the stub at attach it to the interface. */  
public Stub()   
{   
this.attachInterface(this, DESCRIPTOR);   
}   
/**  
 * Cast an IBinder object into an forService interface,  
 * generating a proxy if needed.  
 */  
public static com.styleflying.AIDL.forService    
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.forService))) {   
return ((com.styleflying.AIDL.forService)iin);   
}   
return new com.styleflying.AIDL.forService.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_registerTestCall:   
{   
data.enforceInterface(DESCRIPTOR);   
com.styleflying.AIDL.forActivity _arg0;   
_arg0 =    
com.styleflying.AIDL.forActivity.Stub.asInterface(data.readStrongBinder());  
this.registerTestCall(_arg0);   
reply.writeNoException();   
return  
 true;   
}   
case TRANSACTION_invokCallBack:   
{   
data.enforceInterface(DESCRIPTOR);   
this.invokCallBack();   
reply.writeNoException();   
return true;   
}   
}   
return super.onTransact(code, data, reply, flags);   
}   
private static class Proxy implements com.styleflying.AIDL.forService   
{   
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 registerTestCall(com.styleflying.AIDL.forActivity cb) throws  
 android.os.RemoteException   
{   
android.os.Parcel _data = android.os.Parcel.obtain();   
android.os.Parcel _reply = android.os.Parcel.obtain();   
try {   
_data.writeInterfaceToken(DESCRIPTOR);   
_data.writeStrongBinder((((cb!=null))?(cb.asBinder()):(null)));   
mRemote.transact(Stub.TRANSACTION_registerTestCall, _data, _reply, 0);   
_reply.readException();   
}   
finally {   
_reply.recycle();   
_data.recycle();   
}   
}   
public void invokCallBack() 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_invokCallBack, _data, _reply, 0);   
_reply.readException();   
}   
finally {   
_reply.recycle();   
_data.recycle();   
}   
}   
}   
static final int TRANSACTION_registerTestCall =    
(IBinder.FIRST_CALL_TRANSACTION + 0);   
static final int TRANSACTION_invokCallBack =    
(IBinder.FIRST_CALL_TRANSACTION + 1);   
}   
public void registerTestCall(com.styleflying.AIDL.forActivity cb) throws  
 android.os.RemoteException;   
public void invokCallBack() throws android.os.RemoteException;   
}  
/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: 
D:\\workspace\\AIDLTest\\src\\com\\styleflying\\AIDL\\forService.aidl
 */
package com.styleflying.AIDL;
import Java.lang.String;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Binder;
import android.os.Parcel;
public interface forService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements 
com.styleflying.AIDL.forService
{
private static final Java.lang.String DESCRIPTOR = 
"com.styleflying.AIDL.forService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an forService interface,
 * generating a proxy if needed.
 */
public static com.styleflying.AIDL.forService 
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.forService))) {
return ((com.styleflying.AIDL.forService)iin);
}
return new com.styleflying.AIDL.forService.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_registerTestCall:
{
data.enforceInterface(DESCRIPTOR);
com.styleflying.AIDL.forActivity _arg0;
_arg0 = 
com.styleflying.AIDL.forActivity.Stub.asInterface(data.readStrongBinder());
this.registerTestCall(_arg0);
reply.writeNoException();
return
 true;
}
case TRANSACTION_invokCallBack:
{
data.enforceInterface(DESCRIPTOR);
this.invokCallBack();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.styleflying.AIDL.forService
{
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 registerTestCall(com.styleflying.AIDL.forActivity cb) throws
 android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((((cb!=null))?(cb.asBinder()):(null)));
mRemote.transact(Stub.TRANSACTION_registerTestCall, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public void invokCallBack() 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_invokCallBack, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_registerTestCall = 
(IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_invokCallBack = 
(IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void registerTestCall(com.styleflying.AIDL.forActivity cb) throws
 android.os.RemoteException;
public void invokCallBack() throws android.os.RemoteException;
}
 

兩段代碼差不多,前面基本一樣,從後面看,最後跟著我們在AIDL中自定義的方法,沒有實現。兩個文件各定義一個了接口,這兩個接口分別會在 Activity和Service中使用,在那裡我們將實現自定義的方法。兩個接口中都定義了一個抽象類Stub,實現所在的接口。Stub中又有一個類 Proxy。Stub中有一個static的asInterface()方法,裡面有很多return語句,在mAIDLActivity中調用它時,它返回一個新創建的內部類Proxy對象。

這個Stub對我們來說很有用,它繼承了Binder。Binder有什麼用呢?一個類,繼承了Binder,那麼它的對象就可以被遠程的進程使用了(前提是遠程進程獲取了這個類的對象【對象的引用】,至於如如何獲得看下文),在本例中就是說,如果一個Service中有一個繼承了Stub的類的對象,那麼這個對象中的方法就可以在Activity中使用,對Activity也是這樣。至於Binder的細節,網上有很多貼介紹,看不明白也不影響我們完成這個例子。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved