您的位置:首页 > 移动开发 > Android开发

android-Creating a Content Provider

2015-11-28 18:20 489 查看
Content Provider是数据存储的一种组件,方式;运行在UI线程中

You need to build a content provider if you want to provide one or more of the following features:

You want to offer complex data or files to other applications.
You want to allow users to copy complex data from your app into other apps.

You want to provide custom search suggestions using the search framework.

Content Provider提供数据的两种方式:
1.File data:Data
that normally goes into files, such as photos, audio, or videos. Store the files in your application's private space.
2."Structured"
data:Data that normally goes into a database, array, or similar structure. Store the data in a form that's compatible with tables of rows and columns. A row represents an entity,
such as a person or an item in inventory.


Data design considerations:

. If you want the provider's application to handle intents, also define intent actions,
extras data, and flags. Also define the permissions that you will require for applications that want to access your data.

The
SQLiteOpenHelper
class
helps you create databases, and the
SQLiteDatabase
class
is the base class for accessing databases.

For
working with network-based data, use classes in
java.net
and
android.net
,以及第三方网络包。

Although you can use any name for this column, using
BaseColumns._ID
is
the best choice, because linking the results of a provider query to a
ListView
requires
one of the retrieved columns to have the name
_ID
.

-If you
want to provide bitmap images or other very large pieces of file-oriented data, store the data in a file and then provide it indirectly rather than storing it directly in a table.MIME
type。

Use the Binary Large OBject (BLOB) data type to store data that varies in size or has
a varying structure. For example, you can use a BLOB column to store a protocol
buffer or JSON
structure.

使用Content provider需要设计一些:Designing Content URIs,Designing
an authority,


Designing a path structure,Handling content URI IDs,Content URI patterns。

To help you choose which action to take for an incoming content URI, the provider API
includes the convenience class
UriMatcher
,
which maps content URI "patterns" to integer values.

A content URI pattern matches content URIs using wildcard characters:

*
:
Matches a string of any valid characters of any length.
#
:
Matches a string of numeric characters of any length.

The abstract class
ContentProvider
defines
six abstract methods that you must implement as part of your own concrete subclass:

1), java.lang.String, java.lang.String[], java.lang.String)]query(),2)insert(),3))]update(),4))]delete(),5)getType()
6)onCreate():Initialize
your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until a
ContentResolver
object
tries to access it.。

Your implementation of these methods should account for the following:

All of these methods except
onCreate()
can
be called by multiple threads at once, so they must be thread-safe. To learn more about multiple threads, see the topic Processes
and Threads.
Avoid doing lengthy operations in
onCreate()
.
Defer initialization tasks until they are actually needed. The section Implementing the
onCreate() method discusses this in more detail.
Although you must implement these methods, your code does not have to do anything except return the expected data type. For example, you may want to prevent other applications from inserting data into some tables. To do this,
you can ignore the call to
insert()
and
return 0.

》Notice that these methods have the same signature as the identically-named
ContentResolver
methods.

If you
aren't using an SQLite database as your data storage, use one of the concrete subclasses of
Cursor
.
For example, the
MatrixCursor
class
implements a cursor in which each row is an array of
Object
.
With this class, use
)]addRow()
to
add a new row.

ContentProvider.onCreate()
SQLiteOpenHelper.onCreate()
的比较以及大多数组件的onCreate()
区别于联系。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: