2. Content Providers
Content providers manage access to a
structured set of data.
Encapsulate the data, and provide
mechanisms for defining data security.
Content providers are the standard
interface that connects data in one
process with code running in another
process.
3. Access data in a content
provider
you use the ContentResolver object in your
application's Context to communicate with
the provider as a client.
The ContentResolver object communicates
with the provider object, an instance of a
class that implements ContentProvider.
The provider object receives data requests
from clients, performs the requested action,
and returns the results.
5. Calendar Provider
The Calendar Provider is a repository for a
user's calendar events.
The Calendar Provider API allows you to
perform query, insert, update, and delete
operations on calendars, events,
attendees, reminders, and so on
6. Every content provider exposes a public URI
(wrapped as a Uri object) that uniquely identifies its
data set.
A content provider that controls multiple data sets
(multiple tables) exposes a separate URI for each
one.
All URIs for providers begin with the string "content://".
This identifies the data as being controlled by a
content provider.
The Calendar Provider defines constants for the URIs
for each of its classes (tables). These URIs have the
format <class>.CONTENT_URI. For example,
Events.CONTENT_URI.
7.
8. How To Use
Uses Permissions
ContentResolver
Querying
14. Contacts Provider
It’s a powerful and flexible Android component
that manages the device's central repository of
data about people.
It’s the source of data you see in the device's
(contacts application , or your own application)
and transfer data between the device and online
services.
It’s accommodates a wide range of data sources
and tries to manage as much data as possible for
each person, ( its organization is complex)
Because of this, it's includes an extensive set of
contract classes and interfaces that facilitate both
data retrieval and modification.
15. Contacts Provider
Organization
The Contacts Provider is an Android
content provider component. It maintains
three types of data about a person, each
of which corresponds to a table offered
by the provider
16.
17. The three tables are commonly referred to
by the names of their contract classes
ContactsContract.Contacts table
Rows representing different people, based on
aggregations of raw contact rows.
ContactsContract.RawContacts table
Rows containing a summary of a person's data,
specific to a user account and type.
ContactsContract.Data table
Rows containing the details for raw contact,
such as email addresses or phone numbers.
18. Demo
Open Contact Manager form Samples
Task
Modify List of Contacts add (_ID ,
HAS_PHONE_NUMBER )
19. Content Provider
A content provider manages access to a central
repository of data.
You implement a provider as one or more classes
in an Android application, along with elements in
the manifest file.
One of your classes implements a subclass
ContentProvider, which is the interface between
your provider and other applications.
Although content providers are meant to make
data available to other applications, you may of
course have activities in your application that
allow the user to query and modify the data
managed by your provider.
20. Before You Start Building C.P.
You want to offer complex data or files to
other applications.
You want to allow users to copy complex
data from your app into other apps.
You want to provide custom search
suggestions using the search framework.
21. You don't need a provider to use an
SQLite database if the use is entirely within
your own application.
Editor's Notes
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
//add reminder
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 15);
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);
public static final String[] INSTANCE_PROJECTION = new String[] {
Instances.EVENT_ID, // 0
Instances.BEGIN, // 1
Instances.TITLE // 2
};
// The indices for the projection array above.
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_BEGIN_INDEX = 1;
private static final int PROJECTION_TITLE_INDEX = 2;
alendar beginTime = Calendar.getInstance();
beginTime.set(2011, 9, 23, 8, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2011, 10, 24, 8, 0);
long endMillis = endTime.getTimeInMillis();
Cursor cur = null;
ContentResolver cr = getContentResolver();
// The ID of the recurring event whose instances you are searching
// for in the Instances table
String selection = Instances.EVENT_ID + " = ?";
String[] selectionArgs = new String[] {"207"};
// Construct the query with the desired date range.
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, startMillis);
ContentUris.appendId(builder, endMillis);
// Submit the query
cur = cr.query(builder.build(),
INSTANCE_PROJECTION,
selection,
selectionArgs,
null);
while (cur.moveToNext()) {
String title = null;
long eventID = 0;
long beginVal = 0;
// Get the field values
eventID = cur.getLong(PROJECTION_ID_INDEX);
beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
title = cur.getString(PROJECTION_TITLE_INDEX);
// Do something with the values.
Log.i(DEBUG_TAG, "Event: " + title);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(beginVal);
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));
}
}