Android 中的自定义内容提供商

发布于 2024-08-30 21:45:28 字数 141 浏览 3 评论 0原文

我正在尝试创建一个自定义 ContentProvider,以便多个应用程序(活动?)可以访问它。我对如何执行此操作有几个问题,

如何在代码中声明它是 ContentProvider? 其他应用程序(活动?)如何使用或导入ContentProvider?

I'm attempting to make a custom ContentProvider so that more than one application (activity?) has access to it. I have several questions about how to do this,

How do I declare in the code that it is a ContentProvider?
How do other applications (activities?) use or import the ContentProvider?

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

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

发布评论

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

评论(2

别低头,皇冠会掉 2024-09-06 21:45:28

除了内容提供商的广泛开发人员指南主题之外,您可以看看记事本教程Note Pad 示例代码 了解有关创建您自己的内容提供程序的信息。

Aside from the extensive developer guide topic on content providers, you can look at the Notepad Tutorial and Note Pad sample code for information on creating your own content providers.

沦落红尘 2024-09-06 21:45:28

在提供内容的应用程序的清单中,您将拥有:

<provider android:name="Inventory" android:authorities="com.package.name.Inventory" />

在获取内容的应用程序中

    String inventoryItem = "dunno";
       try {
          Uri getItem = Uri.parse(
            "content://com.package.name.Inventory/database_items");
            String[] projection = new String[] { "text" };
            Cursor cursor = managedQuery(getItem, projection, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
              int index = cursor.getColumnIndex("text");
              inventoryItem = cursor.getString(index));     
            }
            cursor.close();
       } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }

在外部程序中,这将返回 item_socks 下列出的项目,但这只是仅列出一个具有该名称的项目的一般示例。您将查询一个名为database_items 的数据库表,它看起来像:

id |标题 |文字

1 | item_socks | 袜子棕色对红色条纹

inventoryItem 将等于“棕色对红色条纹”

In the manifest of the application providing the content you would have:

<provider android:name="Inventory" android:authorities="com.package.name.Inventory" />

And in the application obtaining content

    String inventoryItem = "dunno";
       try {
          Uri getItem = Uri.parse(
            "content://com.package.name.Inventory/database_items");
            String[] projection = new String[] { "text" };
            Cursor cursor = managedQuery(getItem, projection, null, null, null);
            if (null != cursor && cursor.moveToNext()) {
              int index = cursor.getColumnIndex("text");
              inventoryItem = cursor.getString(index));     
            }
            cursor.close();
       } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }

In an external program, this would return the item listed under item_socks, but this is just a general example of having only one item listed with that name. You would be querying a database table named database_items that looks something like:

id | title | text

1 | item_socks | brown pair red stripe

inventoryItem would then be equal to "brown pair red stripe"

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