显示
Cursor ContentResoler
ContentProvider
1 客户端访问ContentProvider
需要Uri;选取的列的名称;--->用于生成Cursor //还有可能需要列的类型
客户端两个Activity。
ListActivity:用于显示所有数据;
定义Uri,列名等信息
要操作数据的时候发送给EditActivity(附加信息:action,Uri)
EditActivity:用于操作数据(进行数据更新,插入);
定义action;
根据接收到的action和Uri进行操作
前端使用的类说明:
ContentUris.withAppendedId(uri,id);
Activity.managedQuery();
ContentResolver的各种方法
2 数据端ContentProvider
实现ContentProvider接口。
实现方式举例:
(1)表实现BaseColumns接口:
定义Uri,mimeType,列名
(2)ContentProvider接口实现:
构建Uri语法树。UriMatcher //用于进行Uri类型判断和getType()使用
实现方法(增删改查,getType,onCreate) //使用数据库实现。查询使用SQLiteQueryBuilder类进行操作
其中:getType()方法作用返回一个mimeType。
间接解析Intent的时候返回mimeType。和intent所带的Action一起选择下一个Activity
(3)ContentProvider接口实现要在AndroidManifest.xml里声明一个<provider android:name="" android:authority="" />
数据端使用类说明:
Uri
UriMatcher
SQLiteOpenHelper,SQLiteQueryBuilder
3 mimeType
For a single record: vnd.android.cursor.item/vnd.yourcompanyname.contenttype
For multiple records: vnd.android.cursor.dir/vnd.yourcompanyname.contenttype
例如:a request for train record 122, like this URI,
content://com.example.transportationprovider/trains/122
might return this MIME type:
vnd.android.cursor.item/vnd.example.rail
例如: a request for all train records, like the following URI,
content://com.example.transportationprovider/trains
might return this MIME type:
vnd.android.cursor.dir/vnd.example.rail
4 Uri:
(1)主要作用:客户端发送Uri-->Provider-->提供/维护数据
次要作用:Intent的间接解析。使用Action与Data(uri)来判断下一个Activity //此时,下一个Activity必须要配置intent-filter(action,data,category)
uri--><provider>-->ContentProvider实现类-->getType(Uri)-->mimeType
Intent使用mimeType与Action来与AndroidManifest.xml里Activity匹配
(2)组成:
content://: Standard prefix indicating that the data is controlled by a content provider. It's never modified.
authority : identifies the content provider. For third-party applications, this should be a fully-qualified class name (reduced to lowercase) to ensure uniqueness. The authority is declared in the <provider>
element's authorities
attribute:
path: The path that the content provider uses to determine what kind of data is being requested.
id : The ID of the specific record being requested, if any.