Content Providers是Android四大组件之一,扮演者非常重要的角色,看下官方文档对它的解释:
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that
connects data in one process with code running in another process.
可以看到Content providers负责管理结构化数据的访问,Content providers封装数据并且提供一套定义数据安全的机制。Content providers是一套在不同进程间进行数据访问的接口。
Content providers为数据跨进程访问提供了一套安全的访问机制,对数据组织和安全访问提供了可靠的保证。
另外来看看官方文档是如何解释Content Providers的使用的:
When you want to access data in a content provider, you use theContentResolverobject
in your application'sContextto communicate with the provider as a client. TheContentResolverobject
communicates with the provider object, an instance of a class that implementsContentProvider. The provider object receives data
requests from clients, performs the requested action, and returns the results.
意思是当你想要通过Content Providers访问数据时,在应用程序的上下文(Context)中使用ContentResolver对象最为客户端(client)与provider进行交互。ContentResolver对象通过实现抽象类ContentProvider的一个实例来访问provider。Provider对象从客户端(client)接收数据请求,执行请求操作并且返回请求结果。
Android通过Content Provider来管理数据诸如音频、视频、图片和通讯录等。还可以通过ContentProvider来访问SQLite数据库等,下面来看看Content Provider的基本使用。
在此之前,官方文档中给出了一些是否需要需用Content Providers的建议:
Decide if you need a content provider. 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 Providers:
1.你想为其他应用程序提供复杂数据或文件;
2.你想允许用户从你的应用程序中拷贝复杂数据到其他的应用中
3.你想使用搜索框架提供自定义的查询建议功能
Content Provider通过URI(统一资源定位符)来访问数据,URI可以理解为访问数据的唯一地址,URI由authority和数据地址构成,关于authority可以理解成网站地址中的主机地址,而数据地址可以理解成某一个页面的子地址,二者共同构成了一个完整的访问地址,关于authority的命名,官方文档中有这么一句话If your Android package
name iscom.example.<appname>, you should give your provider the authoritycom.example.<appname>.provider.可见对于authority的命名还是有一定的规范性的。
关于URI的格式:content://<authority>/<path>/<id>,path就是数据路径,比如说一张表,而id就是这张表中主键为id的一行,也可以理解成一个实体对象。
看下构建Content Provider和使用URI的代码:
publicclassExampleProviderextendsContentProvider{...
// Creates a UriMatcher object.
privatestaticfinalUriMatcher sUriMatcher;...
/*
* The calls to addURI() go here, for all of the content URI patterns that the provider
* should recognize. For this snippet, only the calls for table 3 are shown.
*/...
/*
* Sets the integer value for multiple rows in table 3 to 1. Notice that no wildcard is used
* in the path
*/
sUriMatcher.addURI("com.example.app.provider","table3",1);
/*
* Sets the code for a single row to 2. In this case, the "#" wildcard is
* used. "content://com.example.app.provider/table3/3" matches, but
* "content://com.example.app.provider/table3 doesn't.
* 这里说明了两种通配符的作用
* *: Matches a string of any valid characters of any length.
* #: Matches a string of numeric characters of any length.
*/
sUriMatcher.addURI("com.example.app.provider","table3/#",2);...
// Implements ContentProvider.query()
publicCursor query(
Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder){...
/*
* Choose the table to query and a sort order based on the code returned for the incoming
* URI. Here, too, only the statements for table 3 are shown.
*/
switch(sUriMatcher.match(uri)){
// If the incoming URI was for all of table3
case1:
if(TextUtils.isEmpty(sortOrder)) sortOrder ="_ID ASC";
break;
// If the incoming URI was for a single row
case2:
/*
* Because this URI was for a single row, the _ID value part is
* present. Get the last path segment from the URI; this is the _ID value.
* Then, append the value to the WHERE clause for the query
*/
selection = selection +"_ID = " uri.getLastPathSegment();
break;
default:
...
// If the URI is not recognized, you should do some error handling here.
}
// call the code to actually do the query }
实现抽象类ContentProvider后,有几个需要实现的方法:
query()
Retrieve data from your provider. Use the arguments to select the table to query, the rows and columns to return, and the sort order of the result. Return the data as aCursorobject.
insert()
Insert a new row into your provider. Use the arguments to select the destination table and to get the column values to use. Return a content URI for the newly-inserted row.
update()
Update existing rows in your provider. Use the arguments to select the table and rows to update and to get the updated column values. Return the number of rows updated.
delete()
Delete rows from your provider. Use the arguments to select the table and the rows to delete. Return the number of rows deleted.
getType()
Return the MIME type corresponding to a content URI. This method is described in more detail in the sectionImplementing Content
Provider MIME Types.
onCreate()
Initialize your provider. The Android system calls this method immediately after it creates your provider. Notice that your provider is not created until aContentResolverobject
tries to access it.
举一个例子,如果使用SQLite作为Content Provider的数据源,下面这段代码片段建立了SQLiteOpenHelper的子类MainDatabaseHelper,并且生成了表main
// A string that defines the SQL statement for creating a tableprivatestaticfinalString SQL_CREATE_MAIN ="CREATE TABLE "+
"main "+ // Table's name
"("+ // The columns in the table
" _ID INTEGER PRIMARY KEY, "+
" WORD TEXT"
" FREQUENCY INTEGER "+
" LOCALE TEXT )";.../**
* Helper class that actually creates and manages the provider's underlying data repository.
*/protectedstaticfinalclassMainDatabaseHelperextendsSQLiteOpenHelper{
/*
* Instantiates an open helper for the provider's SQLite data repository
* Do not do database creation and upgrade here.
*/
MainDatabaseHelper(Context context){
super(context, DBNAME,null,1);
}
/*
* Creates the data repository. This is called when the provider attempts to open the
* repository and SQLite reports that it doesn't exist.
*/
publicvoid onCreate(SQLiteDatabase db){
// Creates the main table
db.execSQL(SQL_CREATE_MAIN);
} }
然后定义ContentProvider来操作数据库,这样,就建立了一套在数据源(SQLite)和客户端(client)之间的接口
publicclassExampleProviderextendsContentProvider
/*
* Defines a handle to the database helper object. The MainDatabaseHelper class is defined
* in a following snippet.
*/
privateMainDatabaseHelper mOpenHelper;
// Defines the database name
privatestaticfinalString DBNAME ="mydb";
// Holds the database object
privateSQLiteDatabase db;
publicboolean onCreate(){
/*
* Creates a new helper object. This method always returns quickly.
* Notice that the database itself isn't created or opened
* until SQLiteOpenHelper.getWritableDatabase is called
*/
mOpenHelper =newSQLiteOpenHelper(
getContext(), // the application context
DBNAME, // the name of the database)
null, // uses the default SQLite cursor
1 // the version number
);
returntrue;
}
...
// Implements the provider's insert method
publicCursor insert(Uri uri,ContentValues values){
// Insert code here to determine which table to open, handle error-checking, and so forth
/*
* Gets a writeable database. This will trigger its creation if it doesn't already exist.
*
*/
db = mOpenHelper.getWritableDatabase();
} }
简单的总结一些Content Providers:
1.Content Providers是Android系统中四大组件之一,提供一套在客户端(client)和数据源(data source)之间的访问接口
2.Content Providers可以提供跨进程访问数据的功能,能暴露本地数据给其他应用访问
3.实现Content Providers只需要继承抽象类ContentProvider并实现必要的抽象方法即可,访问ContentProvider则根据URI来访问
以上简单介绍了下ContentProvider,资料来源是Android官方文档,有不足之处还望指教
参考:http://developer.android.com/guide/topics/providers/content-providers.html
欢迎关注我的新浪微博:@唐韧_Ryan
分享到:
相关推荐
本篇文章将深入探讨"android理论学习——基本概念"中的三个关键要素:Manifest、Content Providers以及Intent和Intent-filter。这些元素构成了Android应用程序的基础架构,使得开发者能够构建功能丰富的移动应用。 ...
在这个案例中,源码是用于创建英语单词记忆程序的代码,包含了项目的各个组成部分,如活动(Activities)、服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)等。 3. **...
1. **项目结构**:MonitorPhone项目通常包含多个模块,如UI界面、服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)。每个模块都有其特定的职责,例如UI界面负责展示数据,...
在这款英语单词记忆程序中,这个文件会定义应用所需的权限(如网络访问权限)以及应用的主要活动(Activities),可能还会有服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)...
这表明内容可能包含了对Android系统理解的深度问题,旨在考察应聘者的Android编程和系统级别的知识。 【描述】"安卓Android源码——2011华为笔试题.zip" 说明这个压缩包文件主要包含的是华为在2011年针对Android...
4. **内容提供者(Content Providers)**:如果音乐播放器需要读取手机上的音乐库,它可能会利用ContentProvider访问Android的媒体存储库,获取歌曲信息,如歌名、歌手、专辑等。 5. **通知(Notifications)**:在...
5. **框架层服务**:如Activity Manager, Package Manager, Content Providers等,它们构成了Android应用架构的基础。 6. **库和工具**:包括各种C/C++库,如SQLite数据库引擎,OpenSSL加密库等,以及构建工具,如...
内容可能涵盖从Android的基础概念,如Activity生命周期,Intent机制,到更高级的主题,如服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)。它还可能包括UI组件的使用,如...
5. **应用程序框架**:提供了一系列API接口,如Content Providers、Intent、Broadcast Receivers、Services等,用于应用程序间的交互。 6. **应用程序**:包括预装和用户安装的各种应用,如电话、短信、浏览器等。 ...
4. **应用程序框架**:这部分源码涵盖了Activity Manager、Content Providers、Broadcast Receivers、Services等组件,它们构成了Android应用的基本架构。通过理解这些框架,开发者可以更好地设计和实现复杂的应用...
- 内容提供者(Content Providers):允许应用间数据共享,如访问联系人数据库。 - 资源管理器(Resource Manager):提供对非代码资源(如字符串、图形和布局文件)的访问。 - 通知管理器(Notification Manager):在...
Android 1.0提供了一个基于组件的服务框架,允许开发者轻松创建和集成各种应用服务,如活动(Activities)、服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)。 3. **数据...
“30.Content Providers(2)”则聚焦于Android四大组件之一——Content Provider。Content Provider是Android系统中数据共享的桥梁,它允许不同应用程序间访问和交换数据。通过Content Provider,你可以将自己的数据...
**第6章:内容提供者**介绍了内容提供者(Content Providers)的作用和用法。内容提供者是一种特殊类型的组件,用于提供数据访问接口,使得其他应用程序能够安全地读取或写入应用的数据。通过本章的学习,读者可以了解...
2. **内容提供者(Content Providers)**:使数据能在不同应用间共享。 3. **意图(Intents)**:用于组件间的通信,触发事件和动作。 4. **活动管理器(Activity Manager)**:管理应用程序生命周期。 5. **通知...
3. **Android应用程序组件**:Android应用由四大组件构成:活动(Activities)、服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content Providers)。这些组件可以独立运行并相互交互。 **第...
4. **高级主题**:深入探讨更高级的主题,如内容提供者(Content Providers)、SQLite与网络服务的集成等,帮助开发者构建功能更强大的应用程序。 5. **案例研究**:通过具体的应用实例来演示如何将理论知识应用于...
内容提供者(Content Providers) 内容提供者(Content Provider)是一种用于访问应用程序之外的数据的组件,例如联系人数据库。这一章节深入探讨了如何创建和使用内容提供者,以及如何处理跨应用程序数据共享的问题...
5. **读取短信**:Android提供了`ContentResolver`和`Cursor`接口,用于访问系统的Content Providers,如SMS Provider。项目中会用到这些接口来查询短信数据库,获取短信列表,并将其绑定到ListView或其他适配器中。...
这一部分会讲述如何在Android中创建和使用SQLite数据库,以及如何使用内容提供者(Content Providers)访问其他应用的数据。 9. **Working in the Background**:为了不干扰用户的操作体验,Android应用需要能夜在...