- Content providers are the standard interface that connects data in one process with code running in another process.
- The provider and provider client automatically handle security and inter-process communication.
ContentResolver
- In the client application's process.
- Owns the provider automatically handle inter-process communication.
- Acts as an abstraction layer between its repository of data and the external appearance of data as tables.
query(Uri, projection, selection, selectionArgs, sortOrder)
Should do queries asynchronously on a separate thread. (ex: CursorLoader class)
- projection: the columns that will be returned for each row
- The expression that specifies the rows to retrieve is split into a selection clause and selection arguments.
- selection:
- The selection clause is a combination of logical and Boolean expressions, column names, and values (the variablemSelection).
- selection 中的值應用 ? 取代,實際的值放在 selectionArgs(the selection arguments array ),作為對應.
- The selection clause is set to null, and the query returns all the words in the provider
Query result:
- If no rows match the selection criteria
- Cursor.getCount() is 0 (an empty cursor).
- If an internal error occurs
- Return null, or it may throw an Exception.
- Since a Cursor is a "list" of rows, a good way to display the contents of a Cursor is to link it to a ListView via a SimpleCursorAdapter.
Content providers can offer the following formats:
You can see the available data types by looking at the Cursor class "get" methods.
- integer
- long integer (long)
- floating point
- long floating point (double)
- Binary Large OBject (BLOB) implemented as a 64KB byte array
Three alternative forms of provider access are important in application development:
- Batch access:
- You can create a batch of access calls with methods in the ContentProviderOperation class, and then apply them with ContentResolver.applyBatch().
- Useful for:
- Inserting a large number of rows.
- Inserting rows in multiple tables in the same method call
- Performing a set of operations across process boundaries as a transaction (an atomic operation).
- Asynchronous queries:
- You should do queries in a separate thread.
- One way to do this is to use a CursorLoader object. The examples in the Loaders guide demonstrate how to do this.
- Data access via intents:
- Although you can't send an intent directly to a provider, you can send an intent to the provider's application, which is usually the best-equipped to modify the provider's data.
* Reference
- Content Providers
- Content Provider Basics
沒有留言:
張貼留言