同一应用程序内的 Android Content Provider

发布于 2024-09-09 03:41:46 字数 99 浏览 3 评论 0原文

我有多个活动(在同一应用程序内)需要访问数据库。实现这个的最佳模式是什么?即使所有活动都属于同一个应用程序,我是否还需要内容提供商?

哪个活动应该负责打开和关闭数据库?

I've more than one activity (inside the same Application) that needs to have access to the database. What's the best pattern to implement this ? Do I need a content provider even if all activities belong to the same application?

Which activity should have the responsibility for opening and closing the database ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-09-16 03:41:46

您的两个选择是内容提供程序或仅使用您自己的数据库抽象层。正如所指出的,如果您需要其他应用程序来共享您的数据,或者您需要连接到 Android 的其他部分(例如快速搜索框架),那么内容提供程序是一种更好的方法。它不应该绑定到 Activity - 应该只是您导入和使用的单独类。

OReilly Android 编程书籍有一章阐释了这两种方法,值得一读。

Your two options are Content Provider or just using your own database abstraction layer. The content provider is a better way to go as pointed out, if you need other apps to share your data or if you need to hook into some other part of Android (like the Quick Search framework). It should not be tied into an Activity - should just be a separate class that you import and use.

The OReilly Android programming book has a chapter which illustrates both approaches, its a good read.

好倦 2024-09-16 03:41:46

没有必要。如果您希望某些外部应用程序访问您的数据,您只需创建一个内容提供程序。

Not necessary. You just have to create a Content Provider if you want some external application to access your data.

喜爱纠缠 2024-09-16 03:41:46

内容提供商提供了一种结构化存储机制,可以仅限于您自己的应用程序或导出以允许其他应用程序访问。如果您不打算向其他应用程序提供对您的 ContentProvider 的访问权限,请在应用程序清单中将它们标记为 android:exported=false。否则,将 android:exported 属性设置为 true 以允许其他应用程序访问存储的数据。

https://developer.android.com/training/articles/security-tips

Content providers offer a structured storage mechanism that can be limited to your own application or exported to allow access by other applications. If you do not intend to provide other applications with access to your ContentProvider, mark them as android:exported=false in the application manifest. Otherwise, set the android:exported attribute to true to allow other apps to access the stored data.

https://developer.android.com/training/articles/security-tips

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文