標籤:android des http java 使用 os
/*在Android中, 每個應用程式都有自己的進程,當需要在不同的進程之間傳遞對象時,該如何?呢? 顯然, Java中是不支援跨進程記憶體共用的。因此要傳遞對象, 需要把對象解析成作業系統能夠理解的資料格式, 以達到跨界對象訪問的目的。在JavaEE中,採用RMI通過序列化傳遞對象。在Android中, 則採用AIDL(Android Interface Definition Language:介面描述語言)方式實現。 AIDL是一種介面定義語言,用於約束兩個進程間的通訊規則,供編譯器產生代碼,實現Android裝置上的兩個處理序間通訊(IPC)。AIDL的IPC機制和EJB所採用的CORBA很類似,進程之間的通訊資訊,首先會被轉換成AIDL協議訊息,然後發送給對方,對方收到AIDL協議訊息後再轉換成相應的對象。由於進程之間的通訊資訊需要雙向轉換,所以android採用代理類在背後實現了資訊的雙向轉換,代理類由android編譯器產生,對開發人員來說是透明的。假設A應用需要與B應用進行通訊,調用B應用中的download(String path)方法,B應用以Service方式向A應用提供服務。需要下面四個步驟: 1> 在B應用中建立*.aidl檔案,aidl檔案的定義和介面的定義很相類,如:在cn.itcast.aidl包下建立IDownloadService.aidl檔案,內容如下:*/
package cn.itcast.aidl;interface IDownloadService {void download(String path);}
/*當完成aidl檔案建立後,eclipse會自動在項目的gen目錄中同步產生IDownloadService.java介面檔案。介面檔案中產生一個Stub的抽象類別,裡麵包括aidl定義的方法,還包括一些其它輔助方法。值得關注的是asInterface(IBinder iBinder),它返回介面類型的執行個體,對於遠程服務調用,遠程服務返回給用戶端的對象為代理對象,用戶端在onServiceConnected(ComponentName name, IBinder service)方法引用該對象時不能直接強轉成介面類型的執行個體,而應該使用asInterface(IBinder iBinder)進行類型轉換。編寫Aidl檔案時,需要注意下面幾點: 1.介面名和aidl檔案名稱相同。 2.介面和方法前不用加存取權限修飾符public,private,protected等,也不能用final,static。 3.Aidl預設支援的類型包話java基本類型(int、long、boolean等)和(String、List、Map、CharSequence),使用這些類型時不需要import聲明。對於List和Map中的元素類型必須是Aidl支援的類型。如果使用自訂類型作為參數或傳回值,自訂類型必須實現Parcelable介面。 4.自訂類型和AIDL產生的其它介面類型在aidl描述檔案中,應該顯式import,即便在該類和定義的包在同一個包中。 5.在aidl檔案中所有非Java基本型別參數必須加上in、out、inout標記,以指明參數是輸入參數、輸出參數還是輸入輸出參數。 6.Java原始類型預設的標記為in,不能為其它標記。//2> 在B應用中實現aidl檔案產生的介面(本例是IDownloadService),但並非直接實現介面,而是通過繼承//介面的Stub來實現(Stub抽象類別內部實現了aidl介面),並且實現介面方法的代碼。內容如下:*/
public class ServiceBinder extends IDownloadService.Stub {@Overridepublic void download(String path) throws RemoteException {Log.i("DownloadService", path);}}
//3> 在B應用中建立一個Service(服務),在服務的onBind(Intent intent)方法中返回實現了aidl介面的對 //象(本例是ServiceBinder)。內容如下:
public class DownloadService extends Service {private ServiceBinder serviceBinder = new ServiceBinder();@Overridepublic IBinder onBind(Intent intent) {return serviceBinder;}public class ServiceBinder extends IDownloadService.Stub {@Overridepublic void download(String path) throws RemoteException {Log.i("DownloadService", path);}}}
//其他應用可以通過隱式意圖訪問服務,意圖的動作可以自訂,AndroidManifest.xml配置代碼如下:
<service android:name=".DownloadService" ><intent-filter><action android:name="cn.itcast.process.aidl.DownloadService" /></intent-filter></service>
/*4> 把B應用中aidl檔案所在package連同aidl檔案一起拷貝到用戶端A應用,eclipse會自動在A應用的gen目錄中為aidl檔案同步產生IDownloadService.java介面檔案,接下來就可以在A應用中實現與B應用通訊,代碼如下:*/
public class ClientActivity extends Activity {private IDownloadService downloadService;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);this.bindService(new Intent("cn.itcast.process.aidl.DownloadService"), this.serviceConnection, BIND_AUTO_CREATE);//綁定到服務}@Overrideprotected void onDestroy() {super.onDestroy();this.unbindService(serviceConnection);//解除服務}private ServiceConnection serviceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {downloadService = IDownloadService.Stub.asInterface(service);try {downloadService.download("http://www.itcast.cn");} catch (RemoteException e) {Log.e("ClientActivity", e.toString());}}@Overridepublic void onServiceDisconnected(ComponentName name) {downloadService = null;}};}