Week6.pptx

COIT20270 Application Development for Mobile Platforms Week 6: Content Providers

Dr. R. Balsys, CQU, 2018.

Source: Beginning Android Programming with Android Studio, J.F. DiMarzio, 2016

Content Providers

Objectives, to :

share data in Android

use a content provider

CQU - COIT20270 Application Development for Mobile Platforms

Content Providers

Content providers are the recommended way of sharing data in Android application

Content providers provide a consistent interface programming interface across all Android applications

Content providers provide much the same services as databases – add, delete, query, edit

but can be stored as databases, files or over a network

Basic Android content providers include;

Browser – bookmarks, history, etc.

CallLog – missed calls, call details, etc.

Contacts – contact details from address book

MediaStore – for audio, video and images

Settings – device settings and preferences

CQU - COIT20270 Application Development for Mobile Platforms

…Content Providers

A URI is used to query a content provider. Query strings always have the form

content:// <authority> / <path> / <id>

where

authority– name of the content provider. For user defined content providers it is the fully qualified name, eg wrox.com.provider

path– specifies the content provider data path to the service provided

id– specifies the actual record required

Eg. Content://contacts/people/4 the fourth person in the people table of the devices contacts database

Table 8.1 in chapter 8 of the DiMarzio text gives further examples

CQU - COIT20270 Application Development for Mobile Platforms

Using a Content Provider

You must first specify the URI of the content provider

Uri myContacts = Uri.parse(“content://contacts/people/”);

Post Honeycomb devices use CursorLoader() to get a cursor instance to the data, but does not block the application thread

The CursorAdapter object maps data from the source to the destination variables. You set up an array of string, String[], for the column variable and an int[] for the views

You then create a new instance of a SimpleCursorAdapter class variable using the column and view values as parameters

Honeycomb and later requires the addition of a FLAG_REGISTER_CONTENT_OBSERVER argument

You also need to set READ_CONTACTS permission in the Android.manifest file

CQU - COIT20270 Application Development for Mobile Platforms

Pre-defined Querystring Parameters

A number of pre-defined Query string constants are defined including:

Browser.BOOKMARKS_URI

CallLog.CONTENT_URI

MediaStore.Images.Media.INTERNAL_CONTENT_URI

MediaStore.Images.Media.EXTERNAL_CONTENT_URI

Settings.CONTENT_URI

Eg Uri contacts= ContactsContract.Contacts.CONTENT_URI;

If you want to retrieve the 1st setting use Uri settings= ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 2);

CQU - COIT20270 Application Development for Mobile Platforms

Printing Cursor objects

Create a method that takes the Cursor variable as input

Use the Cursor classes moveToFirst() and moveToNext() to iterate through the items in the Cursor object

The ContactsContract.Contacts._ID field returns the ID of the contact and the ContactsContract.Contacts.DISPLAY_NAME returns the contacts name

Use ContactsContract.Contacts.HAS_PHONE_NUMBER to determine if the contact has a phone number and then ContactsContract.CommonDataKinds.Phone.NUMBER to access the phone number

CQU - COIT20270 Application Development for Mobile Platforms

Content Providers - Projections

The 3rd parameter of CursorLoader() returns the columns that are returned by the query – this parameter is known as the projection

You can set up an array to contain the columns you wish to return from the data source and pass this as the projection parameter

String[] projection= new String[] {

ContactsContract.Contacts._ID,

ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER

};

CQU - COIT20270 Application Development for Mobile Platforms

Content Providers - Filtering

The 4th and 5th parameters of CursorLoader() enable you to make a WHERE clause enquiry

CursorLoader cL=new CursorLoader(this, contacts, projection, ContactsContract.Contacts.DISPLAY_NAME” + “ LIKE ‘Ron’ ”, null, null);

You can use the next parameter to supply a list strings to use instead

CursorLoader cL=new CursorLoader(this, contacts, projection, ContactsContract.Contacts.DISPLAY_NAME” + “ LIKE ? ”, new String[] {“Ron”, “Mary”}, null);

CQU - COIT20270 Application Development for Mobile Platforms

Content Providers - Sorting

The last parameter of CursorLoader() enables you to specify an ORDER BY clause of a SQL query

CursorLoader cL=new CursorLoader(this, contacts, projection, ContactsContract.Contacs.DISPLAY_NAME” + “ LIKE ? ”, new String[] {“Ron”, “Mary”}, ContactsContract.Contacts.DISPLAY_NAME” + “ ASC ”);

CQU - COIT20270 Application Development for Mobile Platforms

Creating Your Own Content Provider

To create your own content provider you provide a class that extends the ContentProvider base class

You need to override the following methods;

getType() – to return the MIME type of the data for the URI

onCreate() – called when provider created

query() – receives a client request and returns a Cursor object

insert() – inserts a new record in the content provide

delete() – deletes records in the content provider

update() – updates a record in the content provider

You can store your data in the file system, a database, an XML file or through a web service

CQU - COIT20270 Application Development for Mobile Platforms

…Creating Your Own Content Provider

You then need to define a number of strings such as the PROVIDER_NAME, CONTENT_URI, _ID, a UriMatcher instance, and a SQLiteDatabase with its associated constants

The UriMatcher object is used to parse the content URI through the ContentResolver

You write a SQLiteHelperDatabase class to help manage the database

You then write the implementations for getType(), onCreate(), query(), insert(), delete() and update()

Call notifyChange() of the ContentResolver after insert() and delete()

Modify the Android.Manifest file by adding the <provider> element

CQU - COIT20270 Application Development for Mobile Platforms

Using Your Own Content Provider

You need to create an activity to test your content provider

To add to the provider create a ContentValues object and populate it with the required values for your data object

If the ContentProvider is in the same package as your tester you can directly reference the provider fields, otherwise you need to specify the field names directly

For external packages you need to refer to the content URI using a fully qualified URI

You should then write methods to display the contents of you content provider and to test the update() and delete() methods

CQU - COIT20270 Application Development for Mobile Platforms