Content Provider

UI Code is directory interacting with database

CatalogActivity ->
EditorActivity -> Content Provider -> PetDbHelper (Pet with ID 1 has weight 7)

Manage access to a structured set of data
-good abstraction layer between data source & UI code (can add data validation, can modify how data is stored UI code is unaffeted)
-work well with other Android framework classes

UI code
Catalog Activity, Content Resolver, Pet Provider, PetDbHelper SQLiteDatabase

Contacts provider
content://com.android.contacts/contacts
Calendar provider
content://com.android.calendar/events
User Dictionary provider
content://user_dictionary/words

Scheme, Content Authority, Type of data

content authority

1
2
3
4
<provider
    android:name=".data.PetProvider"
    android:authorities="com.example.android.pets"
    android:exported="false" />
1
2
3
4
5
6
int match = sUriMatcher.match(uri);
if (match == CONTACTS){
    // Act on the contacts table
} else if (match == CONTACTS_ID){
    // Act on a single contact in contacts table
}
1
2
3
4
5
6
7
8
9
10
11
12
public class PetProvider extends ContentProvider {
 
    private static final int PETS = 100;
    private static final int PET_ID = 101;
 
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sUriMatcher.addURI(PetContract.CONTENT_AUTHORITY, PetContract.PATH_PETS,PETS);
        sUriMatcher.addURI(PetContract.CONTENT_AUTHORITY,PetContract.PATH_PETS + ""/, PETS_ID);
    }
    ...
}