Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android學習之remote service 的aidl詳解

android學習之remote service 的aidl詳解

編輯:關於Android編程

 

aidl的官方描述:

AIDL (Android Interface Definition Language) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

AIDL(android接口定義語言)與IDLs類似,他主要是定義客戶端與服務器統一的編程接口以便讓客戶端與服務器端跨進程通信(IPC)。

什麼地方使用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.

如果clients要從不同應用跨進程訪問service,在service中處理多線程操作。我們才會使用AIDL。如果不需要跨進程,我們可以創建實現Binder的接口,如果我們需要跨進程通信,但是不需要處理多線程操作,那麼,我們可以使用Messager.

如何使用aidl:

To create a bounded service using AIDL, follow these steps:

1. Create the .aidl file

This file defines the programming interface with method signatures.

2. Implement the interface

The Android SDK tools generate an interface in the Java programming language, based on your .aidl file. This interface has an inner abstract class named Stub that extends Binder and implements methods from your AIDL interface. You must extend the Stub class and implement the methods.

3. Expose the interface to clients

Implement a Service and override onBind() to return your implementation of the Stub class.

aidl使用的詳細步驟

我以官方文檔和另外一個樣例,寫了一個Demo來說明如何實現aidl的使用。
(1)客戶端

(2)服務端

 

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