Monday, February 25, 2013

How ContentProvider works in Android

Why Android uses Content Provider? ContentProvider is just a Android version of passing database between multiple processes. It provides encapsulated data passing, while giving IPC between multiple processes.
Some backgrounds:
  • ContentProvider
  • A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object. Together, providers and provider clients offer a consistent, standard interface to data that also handles inter-process communication and secure data access. Quotes from Android Developer.
    Communication between ContentProvider and ContentResolver can act as encapsulated interface, as well as providing IPC between processes (Client which is using ContentProvider, and process that owns ContentProvider). Accessing ContentProvider sometimes requires special permission, and it should be addressed in the Manifest. Note: If provider does not specify any permission, NO other applications are able to access to the provider.
  • ContentResolver
  • When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. Quotes from Android Developer.
  • Accessing ContentProvider
  • ContentProvider can be accessed by using ContentResolver, from below method.

    public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    API Level 1.
    uri: The URI, using the content://scheme, for the content to retrieve.
    projection: A list of which columns to return. Passing null will return all columns, which is inefficient.
    selection: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
    selectionArgs: You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
    sortOrder: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

    public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)
    API Level 16. Same as above method except 1 parameter cancellationSignal
    cancellationSignal: A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
  • URI
  • TODO
  • ContentUri
  • TODO
  • Parcel
  • TODO
  • Cursor
  • TODO
  • Contract
  • TODO
  • MIME
  • MediaStore
  • TODO
  • ContactProvider
  • TODO
  • CalendarProvider
  • TODO
  • CustomProvider
  • TODO