A Content Provider(CP) is a component that encapsulates data, expose them through a standard API and optionally make these data available to other applications. CP is independent of a data storage, which can be local, remote or mixed.
We need a CP contract class to describe the data exposed from the CP(tables, columns, SQL creation strings, etc). The table information is in nested classes, a separate class for each table. Those classes should describe how to access the table, the table’s URI and the column name. The table URI has 3 parts:
- content:// – scheme
- me.kandz.gameimagedownloader – package name
- tableName – the table name
To create a CP contract class, right-click the package, then click New and Java class. Specify the class name, mark it as final and click OK
Create a private constructor so no instance will be created:
Create the authority string and the authority URI
Create nested class for each exposed table
Create a string for the path
Create an URI constant to access this table
Now it is time to add the column constant. It is a good practice to put the columns constants in protected interfaces and then implement the inner class, in case of identical columns across multiple tables. Also, you should put the common columns in one interface and remove it from the other interface. Create the protected interface and add the column constants.
Implement the interface, together with the baseColumn if you need the _ID column
- Android – Notifications
- Content Provider – Part 3 – insert, delete, update, getType
- Content Provider – Part 2 – create and query