Android - Provider Contract

Card Puncher Data Processing

About

A contract describes how the information of the content provider is stored.

  • For a table, it defines table and column names for the database.
  • For an image, it define the file

Example

Table definition

It defines:

  • The Content authority. The unique name to identify the content provider. A convenient string to use for the content authority is the package name for the app, which is guaranteed to be unique on the device.
public static final String CONTENT_AUTHORITY = "com.example.android.sunshine.app";
  • The Base Content Uri. The base is used to create the base of all URI's which apps will use to contact the content provider.
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
  • Each columns of a row of a table as a static field in a subclass that implements BaseColumns (_ID and _COUNT are then automatically included). This static field can be used after in the UI or in an android helper class such as the sqlite SQLiteOpenHelper. See WeatherDbHelper.java
// Data path (appended to base content URI)
public static final String DATA_PATH = "aliasToTableData"; // Generally just the table Name
// For instance, content://com.example.android.sunshine.app/aliasToTableData/ is a valid path for

/* Inner class that defines the table contents of the location table */
public static final class TableEntry implements BaseColumns {

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

        // Content Type: Return more than one entry (a cursor or a dir)
        // The content type is returned by the getType(Uri) function of the content provider
	public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + DATA_PATH;
        
        // Content Type: Return only one row
        // The content type is returned by the getType(Uri) function of the content provider
	public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + DATA_PATH;

	// Table name
	public static final String TABLE_NAME = "tableName";

	// The columns name
	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";

	// The URI to return a row by Id 
	public static Uri buildLocationUri(long id) {
		return ContentUris.withAppendedId(CONTENT_URI, id);
	}
	
}

Example





Discover More
Card Puncher Data Processing
Android - Sqlite

sqlite supports in Android. Sqlites comes packaged with the Android OS as a C++ library. android/database/sqlite/SQLiteOpenHelperSQLiteOpenHelper. See udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/java/com/example/android/sunshine/app/data/WeatherDbHelper.javaWeatherDbHelper.java...



Share this page:
Follow us:
Task Runner