CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
ListView Examples. Basic Steps for Creating a Listview 1.Create a layout (e.g., a LinearLayout), with elements for –the ListView (
Android – CoNTENT PRoViders
SQLite in Mobile Apps by Dan Youberg. Overview Many well known applications and Internet browsers use SQLite due to its very small size (~250 Kb). Also.
ContentProviders.  Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete.
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.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
Data Persistence in Android
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.
Mobile Application Development Selected Topics – CPIT 490
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.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
CS378 - Mobile Computing Content Providers And Content Resolvers.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
Android Content Providers In Android security model, one application cannot directly access (read/write) other application's data. Every application has.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
SQLite (part deux) 1 CS440. Traditional Model View Controller (MVC) CS440 2.
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.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
David Sutton USING CONTENT PROVIDERS. TOPICS COVERED THIS WEEK  Persistence  Introduction to databases  Content providers  Cursors  Cursor adapters.
CHAPTER 9 File Storage Shared Preferences SQLite.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
Content Providers.
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Content provider.
Android Content Providers & SQLite
Data Storage: Part 4 (Content Providers)
Mobile Software Development for Android - I397
Content Providers And Content Resolvers
SQLite in Android Landon Cox March 2, 2017.
Mobile Software Development for Android - I397
Android Application SQLite 1.
Reactive Android Development
CS499 – Mobile Application Development
Android Database using SQLite
Mobile Application Development Chapter 5 [Persistent Data in Android]
Content Providers.
Mobile Software Development for Android - I397
CIS 470 Mobile App Development
CMPE419 Mobile Application Development
Mobile Computing With Android ACST 4550 Android Database Storage
CIS 470 Mobile App Development
Android Developer Fundamentals V2
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

CONTENT PROVIDER

Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to other apps  Any app with appropriate permission, can read and write the data.  Many native databases are available via the content providers, for example Contact Manager  Common interface for querying the data

About Content Provider  Content providers expose their data as a simple table on a database model  Every record includes a numeric _ID field that uniquely identifies the record within the table.

About Content Provider  Content provider exposes a public URI that uniquely identifies its data set: content:// /[data_path]/[instance identifier]  the URI starts with content:// scheme.  Authority is a unique identifier for the content provider. Example “contacts”.  data path specifies the kind of data requested. For example, “people” is used to refer to all the contacts within “contacts”.

Instance Identifier  there can be an instance identifier that refers to a specific data instance.  content://media/internal/images - return the list of all internal images on the device.  content://media/external/images - return the list of all the images on external storage (e.g., SD card) on the device.  content://call_log/calls - return a list of all the calls registered in the call log.  content://browser/bookmarks - return a list of bookmarks stored in the browser.  content://contacts/people/45 - return the single result row, the contact with ID=45.

Android Native ContentProvider  Browser—Read or modify bookmarks, browser history, or web searches.  CallLog —View or update the call history.  Contacts —Retrieve, modify, or store the personal contacts. Three-tier data model of tables under a ContactsContract object:  ContactsContract.Data —Contains all kinds of personal data.  ContactsContract.RawContacts —Contains a set of Data objects associated with a single account or person.  ContactsContract.Contacts —Contains an aggregate of one or more RawContacts, presumably describing the same person.  MediaStore —Access audio, video, and images.  Setting —View and retrieve Bluetooth settings, ring tones, and other device preferences.

Android Native ContentProvider  Android defines CONTENT_URI constants for all the providers that come with the platform.  ContactsContract.CommonDataKinds.Phone.CONTENT_URI (content://com.android.contacts/data/phones)  Browser.BOOKMARKS_URI (content://browser/bookmarks)  MediaStore.Video.Media.EXTERNAL_CONTENT_URI (content://media/external/video/media)  ContactsContract.Contacts.CONTENT_URI (content://contacts/people)

Querying Native Content Provider  You need  URI ContactsContract.Contacts.CONTENT_URI  Names of data fields (result comes in table) ContactsContract.Contacts.DISPLAY_NAME  Data types of those fields String  Remember to modify the manifest file for permissions! To retrieve the contacts: To retrieve the contacts:

Query import android.provider.Contacts.People; import android.content.ContentUris; import android.net.Uri; import android.database.Cursor; //All contacts Uri allContacts = Uri.parse(“content://contacts/people”); //or Uri allContacts = ContactsContract.Contacts.CONTENT_URI; // Use the ContentUris method to produce the base URI for the contact with _ID == 23. Uri myPerson = Uri.parse(“content://contacts/people/23”); //or Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23); // Alternatively, use the Uri method to produce the base URI. // It takes a string rather than an integer. Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23"); // Then query for this specific record: Cursor cur = managedQuery(myPerson, null, null, null, null);

Query Projection String[] projection = new String[] { People._ID, People._COUNT, People.NAME, People.NUMBER }; // Get the base URI for the People table in the Contacts content provider. Uri contacts = People.CONTENT_URI; // Make the query. Cursor managedCursor = managedQuery(contacts, projection, // Which columns to return null, // Which rows to return (all rows) null, // Selection arguments (none // Put the results in ascending order by name People.NAME + " ASC");

Content Resolver getContentResolver().query(…) getContentResolver().insert(…) getContentResolver().update(…) getContentResolver().delete(…)  Cursor cur = managedQuery(…) is equivalent to Cursor cur = getContentResolver().query(…); startManagingCursor(cur);  Starting HoneyComb (API level 11) CursorLoader cl = new CursorLoader(this, …); Cursor cur = cl.loadInBackground()

Insert/Update/Delete private void insertRecords(String name, String phoneNo) { ContentValues values = new ContentValues(); values.put(People.NAME, name); getContentResolver().insert(People.CONTENT_URI, values); } private void updateRecord(int recNo, String name) { //appending recNo, record to be updated Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, recNo); ContentValues values = new ContentValues(); values.put(People.NAME, name); getContentResolver().update(uri, values, null, null); } private void deleteRecords() { Uri uri = People.CONTENT_URI; getContentResolver().delete(uri, null, null); }

Implementing your own Content Provider Set up a system for storing the data  e.g., SQLite using SQLiteOpenHelper. Extend ContentProvider class to provide access:  query()—Allows third-party applications to retrieve content.  insert()—Allows third-party applications to insert content.  update()—Allows third-party applications to update content.  delete()—Allows third-party applications to delete content.  getType()—Allows third-party applications to read each of URI structures supported.  onCreate()—Creates a database instance to help retrieve the content. Declare the Content Provider in manifest <provider android:name=”BooksProvider” android:authorities=”net.learn2develop.provider.Books”>

Implementing your own Content Provider public class BooksProvider extends ContentProvider { static final String PROVIDER_NAME = “net.learn2develop.provider.Books”; static final Uri CONTENT_URI =Uri.parse(“content://”+ PROVIDER_NAME + “/books”); static final String _ID = “_id”;static final String TITLE = “title”;static final String ISBN = “isbn”; static final int BOOKS = 1;static final int BOOK_ID = 2; private static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(PROVIDER_NAME, “books”, BOOKS); uriMatcher.addURI(PROVIDER_NAME, “books/#”, BOOK_ID); } SQLiteDatabase booksDB; static final String DATABASE_NAME = “Books”; …. private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) {…} public void onCreate(SQLiteDatabase db) {…} public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { …} }

Implementing …

@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); sqlBuilder.setTables(DATABASE_TABLE); if (uriMatcher.match(uri) == BOOK_ID) sqlBuilder.appendWhere(_ID + “ = “ + uri.getPathSegments().get(1)); if (sortOrder==null || sortOrder==””) sortOrder = TITLE; Cursor c = sqlBuilder.query( booksDB, projection, selection, selectionArgs, null, null, sortOrder); //---register to watch a content URI for changes--- c.setNotificationUri(getContext().getContentResolver(), uri); return c; }

Implementing …