1) Create a Contract Class

A contract class defines constants that help applications work with the content URIs, column names, intent actions, and other features of a content provider. Contract classes are not included automatically with a provider; the provider’s developer has to define them and then make them available to other developers.

A provider usually has a single authority, which serves as its Android-internal name. To avoid conflicts with other providers, use a unique content authority. Because this recommendation is also true for Android package names, you can define your provider authority as an extension of the name of the package containing the provider. For example, if your Android package name is com.example.appname, you should give your provider the authority com.example.appname.provider.

public class MyContract {

public static final String CONTENT_AUTHORITY = “com.example.myApp”; public static final String PATH_DATATABLE = “dataTable”; public static final String TABLE_NAME = “dataTable”;

}

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table or file (a path). The optional id part points to an individual row in a table. Every data access method of ContentProvider has a content URI as an argument; this allows you to determine the table, row, or file to access. Define these in the contract class.

public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_DATATABLE).build();

// define all columns of table and common functions required

2) Create the Helper Class

A helper class manages database creation and version management.

public class DatabaseHelper extends SQLiteOpenHelper {

// Increment the version when there is a change in the structure of database public static final int DATABASE_VERSION = 1; // The name of the database in the filesystem, you can choose this to be anything public static final String DATABASE_NAME = “weather.db”;

public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }

@Override public void onCreate(SQLiteDatabase db) { // Called when the database is created for the first time. This is where the // creation of tables and the initial population of the tables should happen. }

@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Called when the database needs to be upgraded. The implementation // should use this method to drop tables, add tables, or do anything else it // needs to upgrade to the new schema version. }

}

3) Create a class that extends ContentProvider class

public class MyProvider extends ContentProvider {

public DatabaseHelper dbHelper;

public static final UriMatcher matcher = buildUriMatcher();
public static final int DATA_TABLE = 100;
public static final int DATA_TABLE_DATE = 101;

A UriMatcher maps an authority and path to an integer value. The method match() returns a unique integer value for a URI (it can be any arbitrary number, as long as it’s unique). A switch statement chooses between querying the entire table, and querying for a single record. Our UriMatcher returns 100 if the URI is the Content URI of Table and 101 if the URI points to a specific row within that table. You can use the \\# wildcard to match with any number and \\* to match with any string.

public static UriMatcher buildUriMatcher() {