java - 什么是契约(Contract)类以及如何使用它

标签 java android android-contentprovider

在最近更新的 Android 开发指南中,内容提供程序的文档包含标题为 Contract Classes 的部分.虽然有一个联系人示例的链接,但目前尚不清楚什么是契约(Contract)类以及如何为我的自定义内容提供程序创建一个

不胜感激。

谢谢!

最佳答案

什么是契约(Contract)类?

契约类是一个 public final 类,它包含 URI、列名、MIME 类型和其他关于 ContentProvider 的元数据的常量定义。它还可以包含 static 辅助方法来操作 URI。

为什么使用它?

  1. 契约(Contract)类在内容之间建立契约(Contract) 提供者和其他应用程序。它确保您的 内容提供者即使有变化也能正确访问 到 URI、列名等的实际值。
  2. 由于它为其常量提供了助记名称,因此开发人员可以 不太可能对列名或 URI 使用不正确的值。
  3. 很容易让 Javadoc 文档可供客户使用 想使用您的内容提供商。

如何使用?

这是一个为包含两个表的天气应用程序设计的契约(Contract)类示例 fragment :天气表和位置表。跳过注释和一些方法以使其保持较小。一般来说,它应该被很好地评论。

public class WeatherContract {

    public static final String CONTENT_AUTHORITY = 
            "com.example.android.sunshine.app";
    public static final Uri BASE_CONTENT_URI = 
            Uri.parse("content://" + CONTENT_AUTHORITY);
    public static final String PATH_WEATHER = "weather";
    public static final String PATH_LOCATION = "location";

    /**Inner class that defines the table contents of the location table. */
    public static final class LocationEntry implements BaseColumns {
        public static final String TABLE_NAME = "location";
        public static final String COLUMN_LOCATION_SETTING = "location_setting";
        public static final String COLUMN_CITY_NAME = "city_name";
        public static final String COLUMN_COORD_LAT = "coord_lat";
        public static final String COLUMN_COORD_LONG = "coord_long";

        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build();

        // Custom MIME types
        public static final String CONTENT_TYPE =
                ContentResolver.CURSOR_DIR_BASE_TYPE +
                        "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;

        public static final String CONTENT_ITEM_TYPE =
                ContentResolver.CURSOR_ITEM_BASE_TYPE +
                        "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;

        // Helper method
        public static Uri buildLocationUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }
    }


    /** Inner class that defines the table contents of the weather table. */
    public static final class WeatherEntry implements BaseColumns {

        public static final String TABLE_NAME = "weather";

        public static final String COLUMN_LOC_KEY = "location_id";
        public static final String COLUMN_DATE = "date";
        public static final String COLUMN_WEATHER_ID = "weather_id";
        public static final String COLUMN_SHORT_DESC = "short_desc";
        public static final String COLUMN_MIN_TEMP = "min";
        public static final String COLUMN_MAX_TEMP = "max";
        public static final String COLUMN_HUMIDITY = "humidity";
        public static final String COLUMN_PRESSURE = "pressure";
        public static final String COLUMN_WIND_SPEED = "wind";
        public static final String COLUMN_DEGREES = "degrees";

        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build();

        public static final String CONTENT_TYPE =
                ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + 
                        "/" + PATH_WEATHER;

        public static final String CONTENT_ITEM_TYPE =
                ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + 
                        "/" + PATH_WEATHER;

        // Helper method.
        public static Uri buildWeatherUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }

        .
        .
        .
    }
}

关于java - 什么是契约(Contract)类以及如何使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9243361/

相关文章:

java - 显示循环读取的第一个图像

Android 内容提供商相同的 URI 不匹配

Java:枚举 values() 和 valueOf(String)

java - Android API 级别 23 中的 ISO 8601 问题

android - 如何在 64 位 Windows 上安装 32 位 Android Studio?

android - 自定义 View : how to set the root layout

java - Android CallLog Content Provider 引发 java.lang.IllegalArgumentException

Android 内容解析器找不到新图像

java - ThreadPoolExecutor - ArrayBlockingQueue ... 在从队列中删除元素之前等待

java - 如何在 Java 中确定另一个进程或可执行文件是 32 位还是 64 位