ContentProviders.  Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
ListView Examples. Basic Steps for Creating a Listview 1.Create a layout (e.g., a LinearLayout), with elements for –the ListView (
Android – CoNTENT PRoViders
Cosc 5/4730 Android Content Providers and Intents.
1 JDBC Java Database Connectivity. 2 c.pdf
컨텐트 프로바이더 박승제. 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.
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.
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
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
Data Storage: Part 3 (SQLite)
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.
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.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
Purdue Pride Joe Gutierrez Tung Ho Janam Jhaveri 4/7/2010Purdue Pride1.
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.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
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.
Database Management System. DBMS A software package that allows users to create, retrieve and modify databases. A database is a collection of related.
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.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 JDBC – Java Database Connectivity CS , Spring 2010.
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.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
Database Programming Code Dissection. Layered Approach Presentation (Activity) DbSampleActivity.java DataAccess (DataSource) CommentsDataSource.java MySQLiteHelper.java.
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.
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Content provider.
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Data Storage: Part 4 (Content Providers)
Content Providers And Content Resolvers
Mobile Software Development for Android - I397
ListView: Part 2.
CS499 – Mobile Application Development
Mobile Application Development Chapter 5 [Persistent Data in Android]
Content Providers.
Mobile Computing With Android ACST Android Database Storage Part 2
ANDROID LISTS.
Mobile Computing With Android ACST 4550 Android Database Storage
Android Developer Fundamentals V2
ListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListAdapter is used to.
Department of School of Computing and Engineering
SQLLite and Android.
plus content providers, loaders, recyclerview
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

ContentProviders

 Databases for reading & writing data  Support typical database operations  e.g., query, insert, update & delete

 Manages & supports ContentProviders  Enables ContentProviders to be used across multiple applications  Provides additional services such as change notification  Use Context.getContentResolver() to access ContentResolver cr = getContentResolver();

 ContentProviders manage data for  Browser – bookmarks, history  Call log- telephone usage  Contacts – contact data  Media – media database  UserDictionary – database for predictive spelling  Many more

 Content providers stored logically as database tables  e.g., artists table in MediaProvider artist_idartist_keyartist 13hashcode1Lady Gaga 44hashcode2Frank Sinatra 45hashcode1Elvis Presley 53hashcode4Barbara Streisand

 ContentProvider identify data sets through URIs, e.g., content://authority/path/id  content:// - data managed by a content provider  authority – id for the content provider  path – 0 or more segments indicating the type of data to be accessed  id – specific record being requested

 Uri for searching contacts database ContactsContract.Contacts.CONTENT_URI = “content://com.android.contacts/contacts/”

 Use ContentResolver.query() to retrieve data  Method returns a Cursor instance for accessing results Cursor query( Uri uri,// ContentProvider Uri String[] projection// Columns to retrieve String selection// SQL selection pattern String[] selectionArgs// SQL pattern args String sortOrder// Sort order )

public class ContactsListExample extends ListActivity { public void onCreate(Bundle savedInstanceState) { ContentResolver cr = getContentResolver(); Cursor c = cr.query( ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null); … } …

public class ContactsListExample extends ListActivity { public void onCreate(Bundle savedInstanceState) { … String columns[] = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.STARRED }; … ContentResolver cr = getContentResolver(); Cursor c = cr.query( ContactsContract.Contacts.CONTENT_URI, columns, ContactsContract.Contacts.STARRED + "= 0 ", null, null); …

 Provides access to query results  Some useful methods  boolean moveToFirst()  boolean moveToNext()  int getColumnIndex(String columnName)  String getString(int columnIndex)

public class ContactsListExample extends ListActivity { public void onCreate(Bundle savedInstanceState) { Cursor c = // issue query List contacts = new ArrayList (); if (c.moveToFirst()) { do { contacts.add(c.getString(c.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME))); } while (c.moveToNext()); …

public class ContactsListExample extends ListActivity { public void onCreate(Bundle savedInstanceState) { … ContentResolver cr = getContentResolver(); Cursor c = // issue query List contacts = new ArrayList (); if (c.moveToFirst()) { do { // add data to contacts variable } while (c.moveToNext()); } …

// populate list view widget ArrayAdapter adapter = new ArrayAdapter (this, R.layout.list_item, contacts); setListAdapter(adapter); }

public class ContactsListExample extends ListActivity { public void onCreate(Bundle savedInstanceState) { … // columns to retrieve String columns[] = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.STARRED }; // columns to display String disp [] = new String[] { ContactsContract.Contacts.DISPLAY_NAME}; // layout for columns to display int[] colResIds = new int[] { R.id.name }; …

ContentResolver cr = getContentResolver(); Cursor c = cr.query( ContactsContract.Contacts.CONTENT_URI, columns, ContactsContract.Contacts.STARRED + "= 0 ", null, null); setListAdapter(new SimpleCursorAdapter( this, R.layout.list_layout, c,colsToDisplay, colResIds)); }

 Use ContentResolver.delete() to delete data public final int delete ( Uri url,// content Uri String where,// SQL selection pattern String[] selectionArgs // SQL pattern args )

public class ContactsListDisplayActivity extends ListActivity { … private void deleteContact(String name) { getContentResolver().delete( ContactsContract.RawContacts.CONTENT_URI, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[] {name}); } private void deleteAllContacts() { getContentResolver().delete( ContactsContract.RawContacts.CONTENT_URI, null, null); } … }

public class ContactsListDisplayActivity extends ListActivity { … private void insertContact(String name) { ArrayList ops = new ArrayList (); …

// create new RawContact ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNT_TYPE, “com.google”).withValue(RawContacts.ACCOUNT_NAME, // add new RawContact ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID, 0 ).withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE).withValue(StructuredName.DISPLAY_NAME, name).build());

public class ContactsListDisplayActivity extends ListActivity { … try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { } catch (OperationApplicationException e) { } … }

 Implement a storage system for the data  Implement a ContentProvider subclass  Declare content provider in manifest

public class MyContentProvider extends ContentProvider { public static final Uri CONTENT_URI = Uri.parse( "content://course.examples.ContentProviders.myContentProvider/"); public static final String _ID = "id”, DATA= "data"; private static final String[] columns = new String[] { _ID, DATA}; private static final Map db = new HashMap (); private static final String contentTypeSingle = "vnd.android.cursor.item/myContentProvider.data.text"; private static final String contentTypeMultiple = ”vnd.android.cursor.dir/myContentProvider.data.text";

public synchronized int delete( Uri uri, String selection, String[] selectionArgs) { String requestIdString = uri.getLastPathSegment(); if (null == requestIdString) { for (DataRecord dr : db.values()) { db.remove(dr.get_id()); } } else { Integer requestId = Integer.parseInt(requestIdString); if (db.containsKey(requestId)) { db.remove(db.get(requestId)); } return // # of records deleted; }

public synchronized Uri insert(Uri uri, ContentValues values) { if (values.containsKey(Data)) { DataRecord tmp = new DataRecord(values.getAsString(Data)); db.put(tmp.get_id(), tmp); return Uri.parse(CONTENT_URI + String.valueOf(tmp.get_id())); } return null; }

public synchronized Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String requestIdString = uri.getLastPathSegment(); MatrixCursor cursor = new MatrixCursor(columns); if (null == requestIdString) { for (DataRecord dr : db.values()) { cursor.addRow(new Object[] {dr.get_id(), dr.get_data()}); } …

else { Integer requestId = Integer.parseInt(requestIdString); if (db.containsKey(requestId)) { DataRecord dr = db.get(requestId); cursor.addRow(new Object[] {dr.get_id(), dr.get_data() }); } return cursor; }

<manifest … package="course.examples.ContentProviders.myContentProvider” …”> …