推荐方法使用AIDL整个过程中的错误处理(Android版) [英] Recommended approach for handling errors across process using AIDL (Android)

查看:3708
本文介绍了推荐方法使用AIDL整个过程中的错误处理(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有粘合剂的服务和客户端生活在不同的进程。使用AIDL,当客户端连接到我的远程粘结剂服务,有我需要传递一个错误(例外)返回给客户端倍。

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.

然而,来自我的理解,这是不可能的。我想从我的粘结剂服务扔的RemoteException,看看会发生什么,我也得到

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.)

在我的logcat中。

in my logcat.

由于它看起来这是不可能的,什么是通知错误的客户最好的办法?我想我可以将我AIDLs使用C风格的界面中,我只返回一个错误code(0成功),但这个长相丑陋。

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.

有没有更好的方法吗?

推荐答案

您远程方法可以返回的 包裹 包含结果数据或异常,如果有一个错误。请参阅<一个href=\"http://developer.android.com/reference/android/os/Parcel.html#writeException%28java.lang.Exception%29\"相对=nofollow> 包裹#writeException 方法。我认为,这是Android的例外情况如何使其恢复执行在的ContentProvider 生活在另一个进程的行动的时候。有很多方法可以返回的结果数据,包括使用捆绑类。

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.

您的经理级可以隐藏unparcelling并返回数据或抛出异常unparcelled,使用户永远不会与包裹

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.

<一个href=\"https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/Parcel.java#L1307\"相对=nofollow>这是对源的链接包裹#writeException

这篇关于推荐方法使用AIDL整个过程中的错误处理(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆