AIDL API級別詳細解釋

AIDL 就是android interface definition language。官方API如是解釋。

(以下所有東西,我挑重點翻譯)

1.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). On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.

這段話說的是啥意思?

AIDL和其他你用過的IDL很類似,這個東西允許你在客戶端和服務端定義的程序接口,這兩個接口通過IPC達成協議來完成對彼此的通信。在android程序中,一個程序不能正常的訪問另一個程序。所以呢,他們需要暴露他們的工程到編譯系統能識別的原始狀態,然後安排兩個程序爲你打破自己的邊界。這個實現的代碼很無聊,所以android使用AIDL實現這些功能。

Before you begin designing your AIDL interface, be aware that calls to an AIDL interface are direct function calls. You should not make assumptions about the thread in which the call occurs. What happens is different depending on whether the call is from a thread in the local process or a remote process. Specifically:

在你定義你的AIDL之前,你必須知道AIDL是接口是直接方法的調用,你不能對在線程中的AIDL調用做出推斷和假設。它的表現形式在本地進程中的某個線程或這遠程進程中是不同的,具體如下:

1.Calls made from the local process are executed in the same thread that is making the call. If this is your main UI thread, that thread continues to execute in the AIDL interface. If it is another thread, that is the one that executes your code in the service. Thus, if only local threads are accessing the service, you can completely control which threads are executing in it (but if that is the case, then you shouldn't be using AIDL at all, but should instead create the interface by implementing a Binder).

1.本地進程中的調用是在同一個線程中執行的。如果這是你的主UI線程,這個線程在AIDL接口中繼續執行。如果是另一個線程,那就是在服務中執行你的代碼的那個。這樣,如果只有本地線程訪問service,你可以完全控制線程在哪個裏面執行(如果是另一種情況,你不能使用AIDL,你應該使用實現Binder的接口代替AIDL)。


發佈了29 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章