Sie sind auf Seite 1von 7

[BLANK_AUDIO]

Hi, I'm Adam Porter.


And this is Programming Mobile
Applications for Android Handheld Systems.
So far in this course we've only talked in
detail about two of the four fundamental
components of Android.
We've talked about activities and we've
talked about broadcast receivers.
Well in this lesson, we'll discuss one
more of these fundamental components:
content providers.
In Android content providers represent
centralized repositories of structured
data.
They encapsulated different data sets and
they specify
and enforce permissions needed to access
that data.
In many ways the content provider class
resembles the databases,
that we talked about in the lesson on data
storage.
Content providers however, were
specifically to allow
data to be shared across different
applications.
Applications that want to access a
particular content provider
do so by using a companion class called
Content Resolver.
ContentResolvers present a database-style
interface that lets applications
read and write the data that is stored
in a content provider, and it therefore,
supports
methods such as query, insert, update,
delete and more.
ContentResolvers also provide additional
services such as notifying
registered observers when data in the
content provider changes.
To use a content resolver, your
application will first get a reference
to the current contexts, ContentResolver,
by
calling the content class's
getContentResolver method.
And the end result is that together,
the content provider and the content
resolver Make
it possible for code running in one
process to access data managed in another
process.
To give some examples.
Android comes with a number of standard
content providers.
For instance, there's a content provider
that stores browser
information such as your bookmarks and
your browsing history.
There's one for keeping track of the
telephone calls, that you make.
And there's one from one managing contact
information, there's
another for keeping track of your
pictures, songs and videos.
And yet another for keeping track of the
words you type into various
applications So that your device can give
better and better predictive typing over
time.
And of course there are many more.
Logically, the data managed by a content
provider is represented as a database
table.
For example, a content provider for
managing a list of artists.
Might have data elements or columns for
each record.
For example, an ID field called _ID, and
an artist name field, in this case called
artist.
Applications identify the data they want
and the
content provider who has that data through
a URI.
The specific content of that URI
identifies a
specific data set that's managed by a
specific ContentProvider.
Let's look at that format.
Content URIs start with the string
content://.
And this is the URI's scheme and it
indicates that the
URI refers to data that is managed by a
content provider.
Next, the URI specifies it's authority
which essentially names
the specific content provider who has the
desired data.
After that the URI can contain 0 or more
segments
which indicate the specific data set
containing the desired data.
And finally URI can have a optional id
which
would indicate a specific record within
the desired data set.
For example, applications that want to
access the
contacts content provider might use the
following URI.
This URI specifies a content scheme.
It specifies com.android.contacts as it's
authority.
Its path has the single string contacts,
which basically
corresponds to a logical table within the
content provider's database.
And in this case, there's no ID field, so
that URI is interpreted as referring
to the entire table, not to a single
record within the table.
An application can use this URI with a
content resolver method, such as the query
method, to retrieve some or all of
the contact records managed by the content
provider.
As you can see, the query method takes
several
parameters, including a URI, something
like what we just saw,
an array of strings which specifies the
specific columns
within the database that should be
retrieved from the ContentProvider.
And several more strings that are used to
perform
the SQL query to be processed by the
ContentProvider.
Now these strings can be used to retrieve
particular subsets of
the data, and to specify an ordering
criteria for the results.
Let's take a look at an application that
uses this method, our
first example application is called
ContentProvider with Cursor Adapter.
This application extracts contact
information from the contacts
ContentProvider.
And then it displays each contact's name
and photo, if they happen to have a photo.
Let's see that application in action.
So, here's my device.
Now I'll start the ContentProvider with
Cursor Adapter application.
As you can see this application is now
presenting
the names and photos of some of my
contacts.
Let's look at the source code for this
application,
so here's the application open in the IDE.
Now I open the main activity.
In on create the code first defines the
columns that it would request from the
contact, ContentProvider.
Now there're of course many different
pieces of data associated with each
contact.
But this application is only interested in
the
contacts name and his or her thumbnail
photo.
Next, the code gets a reference to to
context's ContentResolver.
And after that, the code defines a string
that it uses to filter out contacts
with missing or empty names, or if they
are unstarred.
Now, the code creates a string that will
define a sorting order for the resulting
data, and that means that records will be
sorted in ascending order of their _ID
fields.
Next, the code issues a call to the query
method.
And the parameters for this call, include
the URI, which is
defined by contract class; called
Contacts, the columns
to extract, the where clause to filter out
specific contacts,
and the string defining the sort order.
And this method returns a cursor, which
will allow
the application to iterate over the
results of this query.
And finally the code creates and sets a
new adapter which
will be used by the list view, to display
the contact info.
In this case, the adapter is a contact
info list adapter which is defined by this
application.
Let's open that class now.
In this class's constructor, the code
finds and stores
a default image for contacts that have no
thumbnail photo.
Now, when the List View needs views to
display,
it will first call the newView method to
get
a brand new view, and then it will call
the bindView method to add data to that
view.
Let's look at each of these two methods,
one at a time.
First, the newView method gets a reference
to the LayoutInflater service.
And then, it calls the inflate method to
create the newView based on an XLM
resource.
Now, down in the bindView method, the code
fills in that view.
First, it displays the contact's name
within a text view.
And next, it stores a reference to an
image view within this
view, and it stores the default photo in
the photo bitmap variable.
After that, the code checks whether the is
an actual photo stored for this contact.
And if so, it gets the URI associated with
that photo and
then opens an input stream to read the
photo data into memory.
Next, it turns that data into a bitmap
which it stores in the photo bitmap
variable.
And finally, the code sets the photo
bitmap.
As the image bitmap for the image view
object we saw earlier.
When an application queries a
ContentProvider, that
operation can take a while to complete.
And as we've mentioned in other contexts,
we typically want to avoid doing
intensive operations on the main thread,
because that can negatively affect
application responsiveness.
To prevent this from happening when we
use ContentProviders, Android provides the
CursorLoader class.
The CursorLoader uses an AsyncTask so that
queries will be
performed on a background thread and not
on the main thread.
To use a CursorLoader, your application
will first need
to create an object that implements the
LoaderManager's LoaderCallback interface.
And then, at run time, it will need to
create and initialize
the CursorLoader with the help of
that object that implements the loader
callbacks.
To do this, the application will call the
LoaderManager's initLoader method to
create and initialize a loader.
That method takes several parameters,
including an ID,
arguments, and the object that implements
the LoaderCallbacks.
After initLoader was called, several
callbacks will occur.
The first callback will be to the onCreate
loader method.
In this method, we'll create and return a
new loader for the specified ID.
And when that loader finishes loading its
data, Android calls the next callback
method, onLoadFinished.
This method receives the newly created
loader
and a cursor containing the relevant data.
When a previously created loader is reset,
the onLoaderReset method is called.
In this method, applications will
typically remove any
references they have to the previous
loader's data.
Our next example application is called
ContentProviderWithCursorLoader.
This application produces the same result
as our previous example.
It extracts contact information from the
Android Contacts ContentProvider
and it then displays each contact's name
and photo if the photo is available.
However, this application performs the
ContentProvider Query
in a background thread using the
CursorLoader.
Let's see that application in action.
So here's my device, now I'll start
up the Content Provider with Cursor Loader
Application.
And as promised we see the same data, that
we did with the previous application.
Let's take a look at the source code for
this application
So here's the application, open in the
IDE.
Now, I'll open the main activity.
And so in on create, you'll see that this
application creates and sets an empty
adapter, because there's no
data to show at this point Next, the code
gets loaderManager and then calls the
initLoader method on it.
Passing in the id zero.
Passing in null for the arguments.
And passing in the reference this as the
object to call back as the loader
progresses.
As we said before, the first call back
method to be called will be the
on create loader method and this code sets
up the same filters that we saw before.
We don't want contacts with empty or
missing names
and we don't want contacts that are un
stared.
We also want this date to be sorted in a
ascending order.
By I.D. And then the code creates and
returns a
new CursorLoader, passing in the
information that we just set up.
The query that provides the data for this
loader will be performed on a background
thread.
And when that query is finished, the
onLoad finish method will be called.
This method takes the cursor that is
passed in, and
swaps in into the list adapter, used by
this application.
And finally, when the cursor is about to
be closed, the onLoader reset method is
called.
In this case, that method simply swaps in
a null cursor to the list view's list
adapter.
[BLANK_AUDIO]

Das könnte Ihnen auch gefallen