Android Training (Content Provider)

21
Content Provider Android Training By Khaled Anaqwa

Transcript of Android Training (Content Provider)

Page 1: Android Training (Content Provider)

Content

ProviderAndroid Training

By Khaled Anaqwa

Page 2: Android Training (Content Provider)

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.

Page 3: Android Training (Content Provider)

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.

Page 4: Android Training (Content Provider)

Calendar Provider part of

Android platform

Calendar Provider

Contacts Provider

Page 5: Android Training (Content 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

Page 6: Android Training (Content Provider)

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.

Page 7: Android Training (Content Provider)
Page 8: Android Training (Content Provider)

How To Use

Uses Permissions

ContentResolver

Querying

Page 9: Android Training (Content Provider)

Uses Permissions

<uses-permission

android:name="android.permission.READ_CAL

ENDAR" />

<uses-permission

android:name="android.permission.WRITE_CA

LENDAR" />

Page 10: Android Training (Content Provider)

ContentResolver

ContentResolver cr = getContentResolver();

Uri uri = Calendars.CONTENT_URI;

Page 11: Android Training (Content Provider)

Query Example

Cursor c = cr.query(uri, projection,

selection, selectionArgs, null);

update(updateUri, values, null, null);

insert(Events.CONTENT_URI, values);

Page 12: Android Training (Content Provider)

Demo

Create Event add it to calendar

Adding Reminder

Page 13: Android Training (Content Provider)

Task

Select all events

Page 14: Android Training (Content Provider)

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.

Page 15: Android Training (Content Provider)

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

Page 16: Android Training (Content Provider)
Page 17: Android Training (Content 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.

Page 18: Android Training (Content Provider)

Demo

Open Contact Manager form Samples

Task

Modify List of Contacts add (_ID ,

HAS_PHONE_NUMBER )

Page 19: Android Training (Content Provider)

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.

Page 20: Android Training (Content 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.

Page 21: Android Training (Content Provider)

You don't need a provider to use an

SQLite database if the use is entirely within

your own application.