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.
. 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
helps you create databases, and the
is the base class for accessing databases.
For
working with network-based data, use classes in
Although you can use any name for this column, using
the best choice, because linking the results of a provider query to a
one of the retrieved columns to have the name
-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,
To help you choose which action to take for an incoming content URI, the provider API
includes the convenience class
which maps content URI "patterns" to integer values.
A content URI pattern matches content URIs using wildcard characters:
The abstract class
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
tries to access it.。
Your implementation of these methods should account for the following:
All of these methods except
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
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
return 0.
》Notice that these methods have the same signature as the identically-named
If you
aren't using an SQLite database as your data storage, use one of the concrete subclasses of
For example, the
implements a cursor in which each row is an array of
With this class, use
add a new row.
ContentProvider.onCreate()与
区别于联系。
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
SQLiteOpenHelperclass
helps you create databases, and the
SQLiteDatabaseclass
is the base class for accessing databases.
For
working with network-based data, use classes in
java.netand
android.net,以及第三方网络包。
Although you can use any name for this column, using
BaseColumns._IDis
the best choice, because linking the results of a provider query to a
ListViewrequires
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 APIincludes 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
ContentProviderdefines
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
ContentResolverobject
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
ContentResolvermethods.
If you
aren't using an SQLite database as your data storage, use one of the concrete subclasses of
Cursor.
For example, the
MatrixCursorclass
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()
区别于联系。
相关文章推荐
- 【分享】Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件
- Android验证码简单设计
- Activity 生命周期回顾 onSaveInstanceState()
- Android逆向之旅---静态分析技术来破解Apk
- android生命周期:activity
- Android兼容性问题 -- RelativeLayout.mesure()出现NullPointerException异常
- Android自定义view以及组合view
- android MotionEvent
- Android开发中getleft和getX,getRawX的区别
- Android开源BezierView:仿QQ未读消息99+条的红色气泡
- android学习3——长宽的单位问题dp,px,dpi
- Android 屏幕水平、垂直使用不同的布局xml文件
- Android dialog使用总结
- Android学习笔记-ScollView实现QQ消息界面滑动效果
- Android中关于横竖屏问题
- LinearLayout的layout_weight的使用
- android桌面组件开发
- AndroidRobot目前已经开源了
- Android获取URI的绝对路径的类
- Android 使用模板生成Word文档,支持手机直接查看word