컨텐트 프로바이더 박승제. Content Provider The only way to share data across applications...

Preview:

Citation preview

컨텐트 프로바이더

박승제

Content Provider

The only way to share data across applications

Using content providerUse existing content providers supplied by android

BrowserBookmark, history, …

ContactsAddress, tel number, …

MediaStoreAudio, image, video, …

SettingsGlobal system-level device preferences

Make own content provider & share data

you can query these providers

for the data they contain

Content Provider Basics

all content providers implement a common inter-facefor querying the provider and returning results

it’s an interface that client use indirectly,most generally through ContentResolver objects

you can use the ContentResolver’s methods to interact with whatever content providers you’re interested in

there’s just a single instance of each type of Con-tentProvider

but, it can communicate with multiple ContentRe-solver objects in different applications and pro-cesses

the interaction between processes is handled by the ContentResolver and ContentProvider classes

The data model

Content providers expose their data as a simple tableon a database model

each row is a recordeach column is data of a particular type and meaning

The data model

A query returns a Cursor objectcan move from record to record and column to columnto read the contents of each fieldhas specialized methods for reading each type of datamethods

moveToFirst()moveToNext()moveToPrevious()moveToPosition()getPositionisFirst(), isLast(), isBeforeFirst(), isAfterLast()

URIs

Each content Provider exposes a public URIwrapped as a Uri objectuniquely identifies its data set

Android defines CONTENT_URI constants for all the providers that come with the platform

android.provider.Contacts.Phones.CONTENT_URIandroid.provider.Contacts.Photos.CONTENT_URIandroid.CallLog.Calls.CONTENT_URI

URI summary

Making the query

To query a content provider, you can use either the ContentResolover.query() orthe Activity.managedQuery() method

both methods take the same set of argumentsboth return a Cursor object

Content Provider usage

Modifying dataApplication needs WRITE permission

Adding new recordsAdding new values to existing recordsUpdating existing recordsDeleting records

Content Provider usage

Adding recordsSet up map of (key, value) pairs in a ContentValues ob-jectCall ContentResolver.insert(uri, contentValues)Returned URI: the URI of new (uri + ID)

used to query or modify this record

Content Provider usage

Updating recordsCall ContentResolver.update()

Deleting recordsCall ContentResolver.delete()

URI of a specific rowor URI of the table (android.provider.Conctacts.People.CONTENT_URI)

Thanks for Mobile Programming Chap7 ma-terial written by hykim

Recommended