7
ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

Embed Size (px)

Citation preview

Page 1: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

ANDROID CONTENT PROVIDERSPeter Liu

School of ICT, Seneca College

Page 2: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

Android Content Providers

• Why?• What?• How to access a built-in Android content

provider?• How to create (and access) a content provider?• Lab Exercise

Page 3: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

Android Concept of a Content Provider

• Persistent data sharing among Android applications• SQLite database

• private to an Android app• accessible to one Java package

• Question: How to make it accessible to other Android

apps/Java packages?

Page 4: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

What is a Content Provider?• A standard interface for connecting data in one process

with another process• http://developer.android.com/guide/topics/providers/content-provid

ers.html (Android for Developers)

• Two diagrams (Figures 4-4, 4-5 in Learning Android)• http://libcat.senecacollege.ca/vwebv/holdingsInfo?

searchId=38206&recCount=50&recPointer=1&bibId=315195

• Demo (the Contacts Provider)• a built-in Android content provider

• Further Exploration• http://developer.android.com/guide/topics/providers/contacts-

provider.html (Android for Developers)

Page 5: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

How to access the Contacts provider?

• Concepts: URI, relational tables, SQL statements• Code Walkthrough (with my comments)

• use a ContentResolver client object• Invoke CRUD (create, retrieve, update, and delete) methods

• display query results (or getting data from query results)• request permissions in the manifest file• CursorLoader

• asynchronous loading of data (i.e. a separate thread)

Page 6: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

Content URI (Uniform Resource Identifier)• Concept: identification of data in a content provider

• all the data in a table• a row in a table

• Examples• “content://call_log/calls”• “content://contacts/people”

• Syntax (textbook)• <standard prefix>://<authority>/<data path>/<id>

• <authority>: contacts• <data path>: people

• API Exploration• http://developer.android.com/reference/android/provider/CallLog.html

Page 7: ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College

How to create a content provider?• Demo (the Books Provider)• Procedure (4 steps from Learning Android)

• Create a Java class that is a subclass of the system’s ContentProvider class.

• Declare the CONTENT_URI.• Implement the CRUD methods and getType() method.• Declare the content provider in the manifest file.

• Code Walkthrough (3 Java classes)• Further Exploration

• course wiki: http://zenit.senecac.on.ca/wiki/index.php/Resources_for_Learning_Android_App_Development