content provider content resolver cursor content provider
play

Content Provider Content Resolver Cursor Content Provider Basics - PowerPoint PPT Presentation

Content Provider Content Resolver Cursor Content Provider Basics Content providers is one of Androids core components that enables you to access data of other applications where data is stored in databases or flat files. SQLite


  1. Content Provider

  2. Content Resolver

  3. Cursor

  4. Content Provider Basics • Content providers is one of Android’s core components that enables you to access data of other applications where data is stored in databases or flat files. • SQLite databases can only be used by the app that created it. However you can choose to share the database with other apps by content provider to manage access to this shared database. • You’ll need a content provider if you want to allow other apps to access your database and if you want to perform custom search suggestions in your own app. • Content providers support the four basic operations, normally called CRUD-operations . With content providers those objects simply represent data – most often a record (tuple) of a database – but they could also be a photo on your SD-card or a video on the web. • Android provides some standard content providers to access contacts, media files, preferences and so on.

  5. The standard content providers of Android Provider Since Usage Manages your web-searches, bookmarks and Browser SDK 1 browsing-history. Manages the calendars on the user’s device. CalendarContract SDK 14 CallLog SDK 1 Keeps track of your call history. The old and deprecated content provider for managing contacts. You should only use this Contacts SDK 1 provider if you need to support an SDK prior to SDK 5! Deals with all aspects of contact management. ContactsContract SDK 5 Supersedes the Contacts-content provider. The content provider responsible for all your MediaStore SDK 1 media files like music, video and pictures. Settings SDK 1 Manages all global settings of your device. Keeps track of words you add to the default UserDictionary SDK 3 dictionary.

  6. Content resolver and content provider • You create a content provider in the app that creates the database. This allows other apps to use the database. • Any app wanting to use the database must use a content resolver to do so. • The Content Resolver communicates with the Content Provider

  7. Example to search the Contacts database for a friend’s email address • Ask the content resolver to look for the email address. The content resolver sends a request to the content provider to search for the email address. • The Android system looks for the correct content provider and sends the request to it. • The provider finds the address and returns it to the content resolver. • The content resolver includes methods that you use for database queries and transactions. These methods correspond with the methods included in the content provider: • query() – to search for data in the database • insert() – to add data in the database • delete() – to delete data in the database • update() – to update data in the database • getType() – returns the MIME type of the data in the database

  8. Content URIs • You use content URI to identify data in a content provider. This is the address that you pass to the content resolver when you want to access a database created in another app. content://authority/optionalPath/optionalId four parts: The scheme to use, an authority, an optional path and an optional id. • The scheme for content providers is always “ content ”. The colon and double - slash “://” are a fixed part of the URI -RFC and separate the scheme from the authority. • Authorities : complete address of the provider, unique for each content provider: package name plus the name of your content provider name • The optional path : table name, is used to distinguish the kinds of data your content provider offers. The content provider for Android’s media store, for example, distinguishes between audio files, video files and images using different paths for each of these types of media. • The last element is the optional id , which must be numeric. The id is used whenever you want to access a single record (row) (e.g. a specific video file).

  9. Content URI Examples • <scheme><AUTHORITY><path/ID> • content://com.example.slqitemodule. MyContentProvider/MyContacts_table //You’d use this address to query the whole table • content://com.example.slqitemodule. MyContentProvider/MyContacts_table/12 //You’d use this address to query row 12 in the table

  10. Declare the provider in the AndroidManifest.xml • Register content provider in the manifest file • The Android system now includes this provider in its list of Content Providers • authorities – consists of the <package name >.<your provider’s name> • provider’s name – the name of your ContentProvider class

  11. • Two types of URIs: directory- and id-based URIs . If no id is specified a URI is automatically a directory-based URI. • You use directory-based URIs to access multiple elements of the same type (e.g. all songs of a band). All CRUD-operations are possible with directory-based URIs. • You use id-based URIs if you want to access a specific element. You cannot create objects using an id-based URI – but reading, updating and deleting is possible. • The path of content URIs can contain additional information to limit the scope. The MediaStore content provider for example distinguishes between audio and other types.

  12. CRUD in ProviderResolver Method Usage Deletes the object(s) for the URI delete provided. The URI can be item- or directory-based Inserts one object. The URI must be insert directory-based Queries for all objects that fit the URI. query The URI can be item- or directory- based Updates one or all object(s). The URI update can be item- or directory-based

  13. The arguments of the query method Type Name Usage The URI of the object(s) to URI uri access. This is the only argument that must not be null This String array indicates String[] projection which columns/attributes of the objects you want to access With this argument you can String selection determine which records to return The binding parameters to the String[] selectionArgs previous selection argument If the result should be ordered String sortOrder you must use this argument to determine the sort order

  14. Cursor object: return value of the query method • The cursor is used to navigate between the rows of the result and to read the columns of the current row. Cursors are important resources that have to be closed whenever you have no more use for them – otherwise you keep valuable resources from being released.

  15. Querying for data • UserDictionary is probably the easiest standard provider available and thus well suited for showing the concepts. But by default this provider has no entries, so you better add some words first.

  16. Cursor example ContentResolver resolver = getContentResolver(); String[] projection = new String[]{BaseColumns._ID, UserDictionary.Words.WORD}; Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null); if (cursor.moveToFirst()) { do { long id = cursor.getLong(0); String word = cursor.getString(1); // do something meaningful } while (cursor.moveToNext()); }

  17. Inserting new records • Very often your app needs to insert data to Android built-in content providers because you want to add events to the Calendar provider, people to the Contacts provider, words to the UserDictionary provider and so on. • The correct content URI for inserts can only be a directory-based URI because only these represent a collection of related items. • The values to insert are specified using a ContentValues object. • This object is just a collection of key/value pairs. The keys of your ContentValues object must match columns/attributes of the objects you want to update – otherwise you will get an exception. For all columns of the new object for which no key/value-pair is provided the default value is used – which most often is null.

  18. The arguments of the insert method Type Name Usage The directory-based URI to which to add the URI uri object. This argument must not be null The values for the object ContentValues values to add. This argument also must not be null ContentValues values = new ContentValues(); values.put(Words.WORD, "Beeblebrox"); resolver.insert(UserDictionary.Words.CONTENT_URI, values);

  19. Updating data • The arguments of the update method: ContentValues object and optionally also a where-clause and arguments for this where-clause. • The arguments of the update method Type Name Usage The URI of the object(s) URI uri to access. This argument must not be null The values to substitute the current data with. ContentValues values This argument also must not be null With this argument you String selection can determine which records to update The binding parameters String[] selectionArgs to the previous selection argument

  20. Update example values.clear(); values.put(Words.WORD, "Zaphod"); Uri uri = ContentUris.withAppendedId(Words.CONTENT_URI, id); long noUpdated = resolver.update(uri, values, null, null);

Recommend


More recommend