ContentProviders - iba-s12

Download Report

Transcript ContentProviders - iba-s12

ContentProviders
SQLite Database
• SQLite is a software library that implements aselfcontained, serverless,zeroconfiguration,transactionalSQL database engine.
SQLite is the most widely deployedSQL database
engine in the world. The source code for SQLite is
in thepublic domain
• Unlike client–server database management
systems, the SQLite engine has no standalone
processes with which the application program
communicates
What is SQLite
• SQLite is an Open Source Database which is
embedded into Android. SQLite supports
standard relational database features like SQL
syntax, transactions and prepared statements.
In addition it requires only little memory at
runtime (approx. 250 KByte).
Use in Mobile Devices
• Due to its small size, SQLite is well suited to embedded
systems, and is also included in:
• Apple's iOS (where it is used for the SMS/MMS, Calendar, Call
history and Contacts storage)
• Symbian OS
• Nokia's Maemo
• Google's Android
• RIM's BlackBerry
• Linux Foundation's MeeGo
• Palm's webOS[31]
• However, it is also suitable for desktop operating systems;
Apple adopted it as an option in Mac OS X's Core Data API
from the original implementation in Mac OS X 10.4 onwards,
and also for administration of videos and songs on the
iPhone.
DB design and Implementation
Design and implement a database adapter class to
manage database transactions (ToDoDBAdapter)
Inside the adapter class ,design and implement
adatabase helperclass (toDoDBOpenHelper) to
create tables and handles table upgrades
Theadapterclass–publishes static database
constants–and handles queries and includes
methods for creating,opening,and closing the
database
Provides strongly typed methods for adding remove
and updating items
SQLITE
DATABASE
APPLICATION
1)Open DB
2)Close DB
3)Insert and Remove objects
In a type safe way
DBAdatper Class
That enbales you to
talk to Sqlite database
DBHelper
Class
DBHELPER Class
tasks
Create Tables
Upgrade tables
Cursor Class
• Android uses the Cursor class as a return value
for queries
• Think of the Cursor as a pointer to the
resultset from a database query.Using Cursor
enables android to more efficiently manage
rows and columns as needed
ContentValues object
• We use ontentValues object to store key/value
pairs ,its put() method enables you to insert
key with values of different datatypes.
• CreateEntry (name,email) function created by
you in the activity returns the ID of the
inserted row,that is why you fix its return
types as a long,if en error occurs during the
operation ,it returns -1
SQLite in Android
• SQLite is available on every Android device.
Using an SQLite database in Android does not
require any database setup or administration.
• You only have to define the SQL statements
for creating and updating the database.
Afterwards the database is automatically
managed for you by the Android platform.
Content Providers
• Content Providers share content with
applications across application
boundries,Example of built in Content
Providers are Contacts,MediaStore,Settings
and more
ContentProviders and sharing Data
• An SQLite database is private to the
application which creates it. If you want to
share data with other applications you can use
a ContentProvider
.
• A ContentProvider allows applications to
access data. In most cases this data is stored in
an SQlite database
Content Providers
• A ContentProvider can be used internally in an
application to access data. If the data should be
shared with another application a ContentProvider
allows this.
• The access to a ContentProvider is done via an URI.
The basis for the URI is defined in the declaration of
the ContentProvider in the "AndroidManifest.xml"
file via the android:authorities attribute.
• Many Android datasources, e.g. the contacts, are
accessible via ContentProviders. Typically the
implementing classes for a ContentProviders provide
public constants for the URI's.
Uniform Resource Identifier
• A unifrom resource indentifier is a string of
characters used to identify the name or a
resource on the internet
• URIs enable interaction representation of
resource over network using specific
protocols
• A URI is defined by its syntax and associated
protocols
REPRESENTATIONAL STATE
TRANSFER(REST)
• REST is a style of software architecture for
distributed hypermedia systemssu ch as WWW
●REST architecture consists of clients and servers
●Requests from clients and responses from
servers are built around transfers of
representations of resources ●A resource is any
coherent and meaningfu lconcept that may
addressed ●Arepresentation is a sequence of bits
that capture s the state of a resource
●Architectures that comply with REST standards
are called RESTfull
WHAT IS Content Provider
• A content provider is a wraper around
datasoruce
• A SQLite is an example of a datasource that
can be exposed as a Content Provider.
• A content provider is a REST-like abstraction.
What is Content Provider
• An SQLite database is private to the
application which creates it. If you want to
share data with other applications you can use
a ContentProvider.
• A ContentProvider allows applications to
access data. In most cases this data is stored in
an SQlite database.
• A ContentProvider can be used internally in an
application to access data. If the data should
be shared with another application a
ContentProvider allows this.
What is ContentProvider(continue)
• The access to a ContentProvider is done via an
URI. The basis for the URI is defined in the
declaration of the ContentProvider in the
"AndroidManifest.xml" file via the
android:authorities attribute.
• Many Android datasources, e.g. the contacts,
are accessible via ContentProviders. Typically
the implementing classes for a
ContentProviders provide public constants for
the URI's.
Saving and retreiving Data through
URIs
• Saving and Retreiving data can be only done
through URIs.
• URIs are translated to underlying data access
mechanisms.
• Any application installed on the same device
can use these URIs to manipulate the Data
Data Encapsulation vs Data Access
• It is conceptually easier to think content
provider as Data Encapsulation
• Why?Because content providers require an
data access mechnism(database or netowork
connection )on top of which they run
• The most frequent data access mechanism is
the SQLite database.
When to use Content Providers
• Only if you need to share some data among
different applications.
• For internal Data access you can use
 Preferences
 Files
 SQLite
 Networks
Android Built in Content providers
Several built-in providers are Browser ,CallLog ,
Contacts, MediaStore,Settings
These top-level providers are SQLite databases
encapsulated as content providers
Some of these databases have multiple tables:
for example ,Contacts has People, Phones,
Photos, and Groups tables
List of providers varies from Android OS to
Android Os
Content Providers as web site
ContentProvider as Web Site
 Each content provider registers it self on a
device as a website
 The registration is done with a string called
authority.
 Authority is similar to a domain name
 Authority string is the basis of a set of URI s
the content provider offers
Authority Registration
• Authority must be registered into the
androidmanifest.xml file
Authority Registration
Content URIs
Content URIs
• Data is retreived from a content provider by
invoking a URI.
• Retreived data are set of row and columns in a
Cursor object.
• All content URIs have the same general form
• Content://*/*/*
• Content://com.google.provider.NotePad/note
s/12
Segments of Content URIs
• URI is
content://com.google.provider.NotePad/notes
/12
• content://com.google.provider.NotePad/notes
/12 is the authority string used to locate the
provider in the provider registry.
• notes/12 is the path section specific to that
provider
• Notes and 12 are path segments.
Segments of a Content URI
• It is the content providers responsibility to
document and programmatically hanlde paths
and segments.
• Convention
– The first segment of the path points to a collection
of objects (eg notes)
– The second segment of the path points to a
specific object in that collection(eg 12)
Content URIs
• Content providers provide REST like URLs to
retrieve and manipulate the data.
Built in Content Providers
• content://contacts/people
• content://contacts/people
• The URIs do not have fully qualified names
because they are built in
• All third party content providers are
recommended to have fully qulaified paths.
MIME Types
MIME Types
 A Web site returns a MIME type for a given URL A
Browser starts a URL handling program depending
on the URL's MIME type
 MIME types work on Android in the same way
they work in HTTP : a content provider can be
asked for the MIME type of a given URI
 When asked about the MIME type, the provider
returns a two-part string according to the standard
web MIME conventions
Data Access With URIs
Data Access With URIs