Saturday, August 30, 2014

Native Android Content Providers

Android exposes several native databases using Content Providers
Android.provider package includes classes that can simplify access to many of the most useful providers

Browser
Browser Content Provider to read or modify bookmarks, browser history, or web searches

CallLog
View or update the call history, including both incoming and outgoing calls, together with missed calls and call details like caller ID and call durations

ContactsContract
to retrieve, modify, or store your contacts’ details

MediaStore
centralized, managed access to the multimedia on your device, including audio, video, and images

Settings
access the device’s preferences using the Settings provider

UserDictionary
Access (or add to) the user defined words added to the dictionary for use in IME predictive text input


Using the Media Store Provider

The MediaStore class includes Audio, Video, and Images subclasses, which in turn contain subclasses that are used to provide the column names and content URIs for each media provider

The Media Store segregates media kept on the internal and external volumes of the host device

MediaStore.<mediatype>.Media.EXTERNAL_CONTENT_URI
MediaStore.<mediatype>.Media.INTERNAL_CONTENT_URI

// Get a cursor over every piece of audio on the external volume.
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                                           null, null, null, null); 

// Let the activity manage the cursor lifecycle.
startManagingCursor(cursor);

// Use the convenience properties to get the index of the columns
int albumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM); 
int titleIdx = cursor. getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);

String[] result = new String[cursor.getCount()];
if (cursor.moveToFirst())
  do { 
    // Extract the song title.
    String title = cursor.getString(titleIdx);
    // Extract the album name.
    String album = cursor.getString(albumIdx);

    result[cursor.getPosition()] = title + " (" + album + ")";
  } while(cursor.moveToNext());

Using the Contacts Provider

Android exposes  all the information available from the contacts database to any application granted the READ_CONTACTS permission

Introducing the Contacts Contract Content Provider

Reading Contact Details

Modifying and Augmenting Contact Details


No comments:

Post a Comment