CS 696 Mobile Phone Application DevelopmentFall Semester, 2009Doc 5 Content ProvidersSept 16, 2009Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.References2Google Android Documentation, http://developer.android.com/reference/packages.htmlhttp://developer.android.com/guide/topics/providers/content-providers.htmlCS 683 Class Notes, http://www.eli.sdsu.edu/courses/fall08/cs683/notes/index.htmlAndroid 1.63Released Sept 15AddsCDMAMultiple screen sizesQuick searchText-to-speechNew Gestures framework4Accessing the databaseUsing adb5Al pro 17->adb -s emulator-5554 shell# sqlite3 /data/data/edu.sdsu.cs696/databases/name.dbSQLite version 3.5.9Enter ".help" for instructionssqlite> select * from NAMES;1|Roger2|Rogersqlite>Eclipse DDMS Perspective6You can download/upload files to your emulator7ContentProviderContent Provider8Only way to share data across applicationsCanRetrieveModifyCreateAndroid Native Content Providers9ContactsMediaStore.AudioMediaStore.ImagesMediaStore.VideoSee http://code.google.com/android/reference/android/provider/package-summary.htmlBasic Idea10Datum mapped to a URIURI is used to access data by clientcontent://contacts/people/All contact namescontent://contacts/people/23Contact with _ID = 21Sample Query11myPerson = Uri.parse("content://contacts/people/23");Cursor cur = managedQuery(myPerson, null, null, null, null);Second way to create URIUri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);CONTENT_URI12Content URI can be complexRather than hardcoding exact URI everywhereContent Providers expose the base URI as static fieldNameProvider.CONTENT_URIpublic static final String AUTHORITY = "edu.sdsu.cs696.NameProvider";public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/names");managedQuery13public final Cursor managedQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)projectionWhich columns to returnselectionSQL Where clause with "WHERE"selectionArgsArguments for selectionsortOrderSQL ORDER BY clauseManaged in the sense that the activity will properly deal with the cursor when the activity is paused, etc.managedQuery14Managed PartCursors & Activity StatesCursors can beActiveDeactivatedConsume fewer resourcesdeactivate(), requery()ClosedManagedQueryActivity will manage changing cursor stateSecond Example15// An array specifying which columns to return. string[] projection = new string[] { People._ID, People.NAME, People.NUMBER,};// Get the base URI for People table in Contacts content provider.Uri mContacts = People.CONTENT_URI; Cursor managedCursor = managedQuery( mContacts, projection, //Which columns to return. null, // WHERE clause--we won't specify. People.NAME + " ASC"); // Order-by clause.Example from http://developer.android.com/guide/topics/providers/content-providers.htmlReading Columns16Cursor requires column indexes for accessWe don't want to know the column index for each data elementContentProvider should provide column names as static fieldsPeople.NAMEpublic static final String NAME = "NAME";Use Cursor.getColumnIndex(String columnName)Example17private void getColumnData(Cursor cur){ if (cur.moveToFirst()) { String name; String phoneNumber; int nameColumn = cur.getColumnIndex(People.NAME); int phoneColumn = cur.getColumnIndex(People.NUMBER); do { // Get the field values name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneColumn); // Do something with the values. ... } while (cur.moveToNext()); }}Example from http://developer.android.com/guide/topics/providers/content-providers.htmlAdding a Record18ContentValues values = new ContentValues();values.put(Contacts.People.NAME, "Roger");values.put(Contacts.People.STARRED,1); // 1 = add to favorites//Add Phone NumbersUri uri = getContentResolver().insert(Contacts.People.CONTENT_URI, values);android.content.ContentProvider19Abstract classquery(Uri uri, String[] columns, String selection, String[] selectionArgs, String sortOrder)returns data to the callerinsert(Uri, ContentValues) inserts new data into the content providerupdate(Uri uri, ContentValues values, String selection, String[] selectionArgs) updates existing data in the content providerdelete(Uri uri, String selection, String[] selectionArgs)deletes data from the content providergetType(Uri) returns the MIME type of data in the content providerPrimary Abstract MethodsExample20Show InsertsUpdateDeleteQuerySame example as last lectureUses ContentProviderUsing the content provider setting the index is not used in inserts.Classes21R.javaConstants, autogeneratedDatabaseHelper.javaCreates tables Updating of schemaNameProvider.javaContentProviderPerforms inserts, deletes, updates, queries of dataDatabaseExample.javaUses NameProvider to access dataURI22content://edu.sdsu.cs696.NameProvider/namesUsed to access all namecontent://edu.sdsu.cs696.NameProvider/names/12Access name with ID = 12edu.sdsu.cs696.NameProviderAuthorityUnique string to identify your dataSuggested to use full class nameMIME Type23vnd.sdsu.cursor.dir/vnd.sdsu.nameMIME type for directory (list) of namevnd.sdsu.cursor.item/vnd.sdsu.nameMIME type for single nameAndroidManifest.xml24<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.sdsu.cs696" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <provider android:name="NameProvider" android:authorities="edu.sdsu.cs696.NameProvider" /> <activity android:name=".DatabaseExample" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.sdsu.cursor.item/vnd.sdsu.name"
View Full Document