Recommended approach for handling errors across process using AIDL (Android)

僤鯓⒐⒋嵵緔 提交于 2019-12-22 17:09:04

问题


I have a binder service and a client that live in different processes. Using AIDL, when the client calls into my remote binder service, there are times that I need to relay an error (exception) back to the client.

However, from my understanding, this is not possible. I tried throwing a "RemoteException" from my binder service to see what will happen, and I get

Uncaught remote exception! (Exceptions are not yet supported across processes.)

in my logcat.

Since it looks like this is not possible, what is the best approach for informing the client of an error? I was thinking I can just convert my AIDLs to use C-style interfaces in which I just return an error code (and 0 on success), but this looks ugly.

Is there a better approach?


回答1:


Your remote method can return a Parcel that contains the result data or an Exception if there is an error. See the Parcel#writeException method. I believe that this is how Android exceptions make it back when performing actions on a ContentProvider that lives in another process. There are many ways to return the result data including using the Bundle class.

Your manager class can hide the implementation details by unparcelling and returning the data or throwing the unparcelled exception so users never interact with the Parcel.

Here is a link to the source for Parcel#writeException.



来源:https://stackoverflow.com/questions/15604145/recommended-approach-for-handling-errors-across-process-using-aidl-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!