Android Training (Content Provider)

Post on 17-Jul-2015

115 views 4 download

Transcript of Android Training (Content Provider)

Content

ProviderAndroid Training

By Khaled Anaqwa

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.

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.

Calendar Provider part of

Android platform

Calendar Provider

Contacts Provider

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

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.

How To Use

Uses Permissions

ContentResolver

Querying

Uses Permissions

<uses-permission

android:name="android.permission.READ_CAL

ENDAR" />

<uses-permission

android:name="android.permission.WRITE_CA

LENDAR" />

ContentResolver

ContentResolver cr = getContentResolver();

Uri uri = Calendars.CONTENT_URI;

Query Example

Cursor c = cr.query(uri, projection,

selection, selectionArgs, null);

update(updateUri, values, null, null);

insert(Events.CONTENT_URI, values);

Demo

Create Event add it to calendar

Adding Reminder

Task

Select all events

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.

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

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.

Demo

Open Contact Manager form Samples

Task

Modify List of Contacts add (_ID ,

HAS_PHONE_NUMBER )

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.

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.

You don't need a provider to use an

SQLite database if the use is entirely within

your own application.