Day 15: Content Provider: Using Contacts API

26
Android Application Development Content Provider Ahsanul Karim [email protected] Sentinel Solutions Ltd. http://www.sentinelbd.com

description

 

Transcript of Day 15: Content Provider: Using Contacts API

Page 1: Day 15: Content Provider: Using Contacts API

Android Application DevelopmentContent Provider

Ahsanul Karim [email protected]

Sentinel Solutions Ltd.http://www.sentinelbd.com

Page 2: Day 15: Content Provider: Using Contacts API

Application Building Blocks

•UI Component Typically Corresponding to one screen.

Activity

•Responds to notifications or status changes. Can wake up your process.

IntentReceiver

•Faceless task that runs in the background.

Service

•Enable applications to share data.

ContentProvider

Page 3: Day 15: Content Provider: Using Contacts API

Android Application Anatomy

Activities1. Provides User Interface2. Usually represents a Single Screen

3. Can contain one/more Views4. Extends the Activity Base class

Services1. No User Interface

2. Runs in Background3. Extends the Service Base Class

Application= Set of Android Components

Content Provider1. Makes application data available

to other apps2. Data stored in SQLite database3. Extends the ContentProvider

Base class

Intent/Broadcast Receiver1. Receives and Reacts to broadcast

Intents2. No UI but can start an Activity

3. Extends the BroadcastReceiver Base Class

Page 4: Day 15: Content Provider: Using Contacts API

Android Content Provider

App 1(Dialer)

App 2(Messaging)

App 3(Custom)

App 4(Custom)

Content Provider 1

Data can be shared over different applications

Content Provider Basics

1. There are no common storage area that all Android application can access.2. The only way: to share data across applications: Content Provider3. Content providers store and retrieve data and make it accessible to all

applications.

Content Provider 2

Page 5: Day 15: Content Provider: Using Contacts API

Android Content ProviderContent Provider Basics (Contd.)

Android ships with a number of content providers for common data types: 1. Audio2. Video3. Images4. Personal contact information etc

Content Provider provides the way to share the data between multiple applications.

For example, contact data is used by multiple applications (Dialer, Messaging etc.) and must be stored in Content Provider to have common access.

A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

Content Providerdata

App 1

App 2

Page 6: Day 15: Content Provider: Using Contacts API

Android Content ProviderContent Provider Basics (Contd.)

Here are some of Android's most useful built-in content providers, along with a description of the type of data they're intended to store

Application can perform following operations on content provider -1. Querying data2. Modifying records3. Adding records4. Deleting records

Page 7: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provider Data Model

Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning.

For example, information about people and their phone numbers might be exposed as follows:

Every record includes a numeric _ID field that uniquely identifies the record within the table. IDs can be used to match records in related tables — for example, to find a person's phone number in one table and pictures of that person in another.

A query returns a Cursor object that can move from record to record and column to column to read the contents of each field. It has specialized methods for reading each type of data. So, to read a field, you must know what type of data the field contains.

Page 8: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provide: URI

1. Each content provider exposes a public URI that uniquely identifies its data set. 2. A content provider that controls multiple data sets (multiple tables) exposes a

separate URI for each one. 3. All URIs for providers begin with the string "content://".

The content: scheme identifies the data as being controlled by a content provider.

URI samples:<standard_prefix>://<authority>/<data_path>/<id>

For example, to retrieve all the bookmarks stored by our web browsers (in Android):content://browser/bookmarks

Similarly, to retrieve all the contacts stored by the Contacts application:content://contacts/people

To retrieve a particular contact, you can specify the URI with a specific ID:content://contacts/people/3

Page 9: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provide: URI

So we need three pieces of information to query a content provider:1. The URI that identifies the provider2. The names of the data fields you want to receive3. The data types for those fields

If we are querying a particular record, you also need the ID for that record.

Some more examples:content://media/internal/images URI return the list of all internal images on the device.content://contacts/people/ URI return the list of all contact names on the device.content://contacts/people/45 URI return the single result row, the contact with ID=45.

Page 10: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provide: URI•Although this is the general form of the query, query URIs are arbitrary and confusing. •For this android provide list of helper classes in android.provider package that define thesequery Strings.

So we do not need to know the actual URI value for different data types. So it will be easy to query data.

content://media/internal/images/ MediaStore.Images.Media.INTERNAL_CONTENT_URIcontent://contacts/people/ Contacts.People.CONTENT_URIcontent://contacts/people/45

Uri person = ContentUris.withAppendedId(People.CONTENT_URI, 23);

To query about specific record we have to use same CONTENT_URI and must append specific ID.

Content Provider: QueryHere is how we can query for data:

Cursor cur = managedQuery(uri, null, null, null);

Page 11: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provider: Query

Here is how we can query for data:Cursor cur = managedQuery(uri, null, null, null, null);

Parameters:1. URI2. The names of the data columns that should be returned. A null value returns all

columns.3. A filter detailing which rows to return, formatted as an SQL WHERE clause

(excluding the WHERE itself). A null value returns all rows.4. Selection arguments5. A sorting order for the rows that are returned, formatted as an SQL ORDER

BY clause (excluding the ORDER BY itself). A null value returns the records in the default order for the table, which may be unordered.

Page 12: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provider: Query ExampleCreate a new project:

Project Name: ContentProviderDemo1Build Target: Android 1.6Application Name:

ContentProviderDemo1Package Name:

com.basistraining.cpdemoCreate Activity: ContentProviderDemoActivityMin SDK Version: 4

Add READ_CONTACTS permission

Now we make the Activity a ListActivity to show a list of contacts

Page 13: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data

Content Provider: Query ExampleNow we make the Activity a ListActivity to show a list of contacts:

Now, we’ll add a method to retrieve data from Contacts Content Provider. The method will return an array of String which will be set as adapter for ListActivity

Page 14: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data Content Provider: Query Example

Now, we’ll add a method to retrieve data from Contacts Content Provider. The method will return an array of String which will be set as adapter for ListActivity

Page 15: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data Content Provider: Query Example

Now, we need to show data in list:

Page 16: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data Content Provider: Query Example

Now, We run the app and in emulator we see a black screen as the emulator has no contact data:

Lets add some contacts using Contacts app

Page 17: Day 15: Content Provider: Using Contacts API

Android Content ProviderQuerying Data Content Provider: Query Example

If we run the app again:

What we are doing here???

We are accessing the content providerContacts from our application.

Contacts application inserts data into Contact Content Provider.

App 1(Contacts)

App 2(our app)

Contacts Content Provider

Page 18: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Let’s add another Activity which will load with selected data and provide us interface to edit:

1. We create Update Activity2. We modify the main.xml for updating

Page 19: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Let’s get back to ListActivity:1. To identify which data to be updated we’ll need to pass _ID of the selected Person. So we

Modify the columns

2. We need to save the values in global arrays to pass them as parameter to update screen.So:

Page 20: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

We need to save the values in global arrays to pass them as parameter to update screen.So:

Page 21: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Now we detect selected person and pass the values to Update screen:

And in the UpdateActivity, we get the values and set them in UI:

Page 22: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Now lets add button functionality of Update

Need the method updateName()

Page 23: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Now lets run the app and we end up with:

WHY????

Page 24: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Now lets run the app and we end up with:

We need to add WRITE_CONTACTS permission

Page 25: Day 15: Content Provider: Using Contacts API

Android Content ProviderModifying Data Content Provider: Update Example

Now follow the steps for testing:

1. Run the app2. Select a Contact3. Edit Contact name4. Go back to main screen5. Exit app6. Check whether data is updated from the Contacts app

Page 26: Day 15: Content Provider: Using Contacts API

Android Content ProviderPractice Exercise

1. Create an Application named “Bookmark Manager”2. In AVD, browse several sites in browser and bookmark them3. Read all bookmarks from browser Content Provider and show them in list4. On selecting an URL go to a new activity and open that link in a webview

Hints

URI: android.provider.Browser.BOOKMARKS_URIFields:

1. Browser.BookmarkColumns._ID, 2. Browser.BookmarkColumns.TITLE, 3. Browser.BookmarkColumns.URL