2

我正在尝试制作一个自定义 ContentProvider 以便多个应用程序(活动?)可以访问它。我有几个关于如何做到这一点的问题,

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

4

2 回答 2

4

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

于 2010-05-04T07:31:56.757 回答
0

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

<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 的数据库表,该表类似于:

编号 | 标题 | 文本

1 | item_socks | 棕色对红色条纹

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

于 2010-10-27T15:00:21.510 回答