Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
ListView Examples. Basic Steps for Creating a Listview 1.Create a layout (e.g., a LinearLayout), with elements for –the ListView (
Android – CoNTENT PRoViders
ContentProviders.  Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete.
PHP (2) – Functions, Arrays, Databases, and sessions.
Cosc 5/4730 Android Content Providers and Intents.
컨텐트 프로바이더 박승제. Content Provider The only way to share data across applications Using content provider Use existing content providers supplied by android.
CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
Data Persistence in Android
Data Storage: Part 1 (Preferences)
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Database Rung-Hung Gau Department of Computer Science and Engineering
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
ContentProviders. SQLite Database SQLite is a software library that implements aself- contained, serverless,zero- configuration,transactionalSQL database.
ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.
Content providers Accessing shared data in a uniform way 1Content providers.
Cosc 5/4730 Android Content Providers and Intents.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Mobile Application Development using Android Lecture 2.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
Android Content Providers In Android security model, one application cannot directly access (read/write) other application's data. Every application has.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
COMP 365 Android Development.  Every android application has a manifest file called AndroidManifest.xml  Found in the Project folder  Contains critical.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
CHAPTER 9 File Storage Shared Preferences SQLite.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
1. 2 The Address Book app provides convenient access to contact information that’s stored in a SQLite database on the device. You can: scroll through.
Content Providers.
Making content providers
Data Storage: Part 3 (SQLite)
Content provider.
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Data Storage: Part 4 (Content Providers)
Content Providers And Content Resolvers
SQL – Python and Databases
SQLite in Android Landon Cox March 2, 2017.
ListView: Part 2.
Mobile Applications (Android Programming)
CS499 – Mobile Application Development
Content Providers.
Mobile Computing With Android ACST Android Database Storage Part 2
Developing a Model-View-Controller Component for Joomla Part 3
Android Developer Fundamentals V2
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
SQL – Application Persistence Design Patterns
Presentation transcript:

Data Storage: Part 4 (Content Providers)

Content Providers Content providers allow the sharing of data between applications. Inter-process communication (IPC) and content providers are the only ways to share application data. Content providers are one of the primary building blocks of Android applications (along with activities, services, and broadcast receivers). A content provider is implemented as a subclass of class ContentProvider (in package android.content ). Android ships with a number of content providers for common data types (audio, video, images, contacts, etc.) –See documentation for package android.provider. –Most of these providers require permission to read their data. Slide 2©SoftMoore Consulting

Selected Android Content Providers Browser – bookmarks, browser history, etc. Contacts – names, phone numbers, etc. Media Store – audio, video, images, etc. Calendar – user’s calendar events Settings – Bluetooth settings, ringtones, Wi-Fi settings, locale, etc. User Dictionary – user-defined words for use with predictive text input. Slide 3©SoftMoore Consulting

Overview of Content Providers Applications can extend the abstract class ContentProvider (in package android.content ) and override the appropriate methods to allow shared access to their data. Regardless of how a content provider actually stores its data, it exposes that data as though it were a simple database table. Note: Every record must include a numeric _id field that uniquely identifies the record within the table. Content providers encapsulate data and make it available to other applications though the ContentResolver interface. Slide 4©SoftMoore Consulting

Overview of Content Providers (continued) Accessing the data exposed by a content provider is very similar to accessing the data in a SQLite database via the convenience methods except that the content provider methods have a URI as their first parameter rather than a database. Content providers are useful for building applications with databases even if the data is not shared outside of the application. A content provider must be declared in the application manifest ( AndroidManifest.xml ) using a element, similar to the way that each activity must be declared. Slide 5©SoftMoore Consulting

Example: Querying a SQLite Database Versus Querying a Content Provider Querying a SQLite database using convenience methods SQLiteOpenHelper openHelper =... ; SQLiteDatabase db = openHelper.getReadableDatabase(); String tableName =... ; String[] columns =... ; String orderBy =... ; Cursor cursor = db.query(tableName, columns, null, null, null, null, orderBy); Querying a content provider ContentResolver cr = getContentResolver(); Uri uri =... ; String[] columns =... ; String orderBy =... ; Cursor cursor = cr.query(uri, columns, null, null, null, null, orderBy); Slide 6©SoftMoore Consulting

Example: Inserting into a SQLite Database Versus Inserting into a Content Provider Inserting into a SQLite database using convenience methods SQLiteOpenHelper openHelper =... ; SQLiteDatabase db = openHelper.getWritableDatabase(); String tableName = "... "; ContentValues values = new ContentValues(); values.put("... ", "... ");... // additional values.put() statements db.insert(tableName, null, values); Inserting into a content provider ContentResolver cr = getContentResolver(); Uri uri =... ; ContentValues values = new ContentValues(); values.put("... ", "... ");... // additional values.put() statements cr.insert(uri, null, values); Slide 7©SoftMoore Consulting

URIs Each content provider exposes a public URI (Uniform Resource Identifier) that uniquely identifies its data set. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one, and possibly joins of the tables. Classes Uri and Uri.Builder (in package android.net ) provide support for creating and accessing URIs for content providers. Slide 8©SoftMoore Consulting

URI Format The URI for a content provider is a string of the form content://authority/path/id content:// – the standard required prefix that identifies the data as being controlled by a content provider. authority – a string that uniquely identifies the content provider; e.g., the provider package name with table name. path – zero or more segments, separated by a forward slash (/), that identify some subset of the provider’s data. Most providers use the path part to identify individual tables. id – the identifier of the specific record being requested. If the request is not limited to a single record, this segment and the trailing slash are omitted. Slide 9©SoftMoore Consulting

URI Constants A content provider usually defines constants in a “contract” class for its URIs to simplify client code and make future updates cleaner. public static final Uri CONTENT_URI =...; The URI constant is used in all interactions with the content provider. Android defines constants for all the providers that come with the platform. –Media Store classes (e.g., MediaStore.Audio.Albums ) define two constants their data, INTERNAL_CONTENT_URI and EXTERNAL_CONTENT_URI –Class Browser defines two constants for its data, BOOKMARKS_URI and SEARCHES_URI. Slide 10©SoftMoore Consulting

Content URI Examples content://user_dictionary/words –UserDictionary.Words.CONTENT_URI content://user_dictionary/words/4 –row from user dictionary with _id = 4 –can be constructed as ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4); content://edu.citadel.android.emergency/emergency_contacts –EmergencyContract.CONTENT_URI (sample application) Slide 11©SoftMoore Consulting Applications usually just use content URI constants declared in in a contract class.

Class ContentResolver Content providers encapsulate data and make it available to other applications though the ContentResolver interface. When a request is made via a ContentResolver the system inspects the authority of the given URI and passes the request to the content provider registered with the authority. The content provider can interpret the rest of the URI however it wants. Most ContentResolver methods take the URI as their first parameter. The URI identifies which provider the ContentResolver should talk to and which table of the provider is being targeted. Slide 12©SoftMoore Consulting

Class ContentProvider Class ContentProvider is an abstract class in package android.content. Class ContentProvider contains several abstract methods that resemble the convenience methods use to access a SQLite database. To create a content provider, extend this abstract class and provide implementations for the abstract methods (shown on next slide). Slide 13©SoftMoore Consulting

Abstract Methods in Class ContentProvider onCreate() –used to initialize the provider query(Uri, String[], String, String[], String) –returns data to the caller insert(Uri, ContentValues) –inserts new data into the content provider update(Uri, ContentValues, String, String[]) –updates existing data in the content provider delete(Uri, String, String[]) –deletes data from the content provider getType(Uri) –returns the MIME type of data in the content provider Slide 14©SoftMoore Consulting

Content Provider Permissions A content provider application can specify permissions (as part of the element in the application manifest) that other applications must have in order to access the provider’s data. Other applications request the permissions they need in order to access the provider. End users see the requested permissions when they install the application. If a content providers application doesn’t specify any permissions, then other applications have no access to the provider's data. However, components within the application always have full read and write access, regardless of the specified permissions. Slide 15©SoftMoore Consulting

Querying a Content Provider Querying a content provider requires three pieces of information: –The URI that identifies the provider –The names of the data fields you want to receive –The data types for those fields It is also possible to query a particular record if you know the ID for that record. Two methods can be used to query a content provider: –ContentResolver.query() –Activity.managedQuery() Slide 16©SoftMoore Consulting Activity.managedQuery() was deprecated in API 11 with the recommendation to use ContentResolver.query() along with CursorLoader and LoaderManager.

Example: Querying Contacts for the Display Name Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] columns = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; String orderBy = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(uri, columns, null, null, orderBy); Slide 17©SoftMoore Consulting

Creating a Content Provider To create a content provider you must: Set up a system for storing the data (e.g., a SQLite database). Extend the ContentProvider class to provide access. –Override the abstract methods onCreate(), insert(), query(), etc. –Note: Some methods can have trivial implementations. For example, if other applications are only allowed to read content, only methods onCreate() and query() need nontrivial implementations. Declare the content provider in the application’s manifest file ( AndroidManifest.xml ). Slide 18©SoftMoore Consulting

Creating a Content Provider (continued) To create a content provider you should: Define a public static final Uri named CONTENT_URI that represents the full “ content: ” URI managed by the content provider. To guarantee that the string has a unique value, use the fully-qualified class name of the content provider converted to lowercase. Define the column names that the content provider will return to clients; e.g., the column names of the underlying database. Also define public static String constants that clients can use to specify the columns in queries and other instructions. Be sure to include an integer column named _id. Slide 19©SoftMoore Consulting

Creating a Content Provider (continued) Carefully document the data type of each column. If you are handling a new data type, define a new MIME type to return in your implementation of ContentProvider.getType(). –There is one form of the MIME type for a single record and another for multiple records. (See the link for “Content Providers” at the end of this section). –Use the Uri methods to help determine what is being requested. Slide 20©SoftMoore Consulting

Example: Content Provider for Emergency Contacts in Previous Section public class EmergencyProvider extends ContentProvider { private static final String AUTHORITY = EmergencyContract.AUTHORITY; private static final String TABLE_NAME = EmergencyContract.TABLE_NAME; private static final Uri CONTENT_URI = EmergencyContract.CONTENT_URI; public static final int CONTACTS = 1; // for contacts table public static final int CONTACTS_ID = 2; // for single contact private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);... } Slide 21©SoftMoore Consulting See handout EmergencyProvider.java. Pay special attention to use of UriMatcher.

Relevant Links Content Providers Android Content Providers An Android Content Provider Tutorial Loading Contacts with Content Providers Slide 22©SoftMoore Consulting