在Android自定义内容提供商中处理(跨进程)异常 [英] Dealing with (cross-process) exceptions in Android custom content provider

查看:206
本文介绍了在Android自定义内容提供商中处理(跨进程)异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序中有一个自定义内容提供程序,工作相当不错。我希望其他应用程序也可以访问我的内容提供商。我想要一些干净的方法来传递异常和错误,但据我所知,Android内容提供程序框架并没有提供任何方式来跨进程传播异常。



我应该如何向我的来电显示异常状态?我必须以某种方式将其编码到我的返回数据中,并依靠客户端来检查它吗?有什么办法吗?如果编码成普通的返回数据是什么,最好的方法是什么? (我可以看到查询调用返回的 Cursor 的一些替代方案,但是$ code> Uri 由插入 int c>更新或删除?)

解决方案

p>有一个 RuntimeException 子类的简短列表,如果在提供程序中抛出,将在客户端应用程序中重新抛出。这些是:




  • IllegalStateException

  • IllegalArgumentException

  • NullPointerException

  • SecurityException

  • BadParcelableException



资料来源:创建内容提供商提到 IAE NPE ;我猜其他人会根据 Parcel.writeException 的Javadoc。



客户端应用只会收到详细信息,而不是堆栈跟踪或原因栈。对于可以编码为 String (详细信息)的特殊状态,这是一个合理的选择。



我仍然对其他解决方案感兴趣。


I have a custom content provider in my Android app that works reasonably well. I expect other apps to also access my content provider. I would like some clean way to communicate exceptions and errors, but as far as I can tell the Android content provider framework doesn't provide any way to propagate exceptions across processes.

How should I indicate an exception state to my caller? Do I have to somehow encode it into my returned data and rely on clients to check for it? Is there any alternative? If encoding into the ordinary return data is it, what's the best way? (I can see a number of alternatives for the Cursor returned by a query call, but what about the Uri returned by insert, or the int returned by update or delete?)

解决方案

There is a short list of RuntimeException subclasses which, if thrown in the provider, will be re-thrown in a client app. These are:

  • IllegalStateException
  • IllegalArgumentException
  • NullPointerException
  • SecurityException
  • BadParcelableException

Source: Creating Content Providers mentions IAE and NPE; I guessed that the others would work based on the Javadoc forParcel.writeException.

The client app will only get the detail message, not a stack trace or the cause stack. For exceptional state which can be encoded into a String (the detail message), this is a reasonable choice.

I'm still interested in other solutions, too.

这篇关于在Android自定义内容提供商中处理(跨进程)异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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