public class MockContentProvider
extends ContentProvider
| java.lang.Object | ||
| android.content.ContentProvider | ||
| android.test.mock.MockContentProvider | ||
模拟ContentProvider的实现。 所有方法都不起作用,并且抛出UnsupportedOperationException 。 测试可以扩展这个类来实现测试所需的行为。
Inherited constants |
|---|
android.content.ComponentCallbacks2
|
Public constructors |
|
|---|---|
MockContentProvider(Context context) 构造函数接受一个Context实例,该实例应该是 |
|
MockContentProvider(Context context, String readPermission, String writePermission, PathPermission[] pathPermissions) 一个构造函数初始化 |
|
Protected constructors |
|
|---|---|
MockContentProvider() 一个使用 |
|
公共方法(Public methods) |
|
|---|---|
ContentProviderResult[] |
applyBatch(ArrayList<ContentProviderOperation> operations) 重写此操作以处理执行一批操作的请求,或者默认实现将迭代操作并在每个操作上调用 |
void |
attachInfo(Context context, ProviderInfo info) 在被实例化之后,这被称为告诉内容提供者自己。 |
int |
bulkInsert(Uri uri, ContentValues[] values) 如果你不愿意手动实现,请调用super.bulkInsert()。 |
int |
delete(Uri uri, String selection, String[] selectionArgs) 实现这个来处理删除一行或多行的请求。 |
String[] |
getStreamTypes(Uri url, String mimeTypeFilter) 由客户端调用以确定此内容提供程序支持的给定URI的数据流类型。 |
String |
getType(Uri uri) 实现这个来处理给定URI处的数据的MIME类型请求。 |
Uri |
insert(Uri uri, ContentValues values) 实现这个来处理插入新行的请求。 |
boolean |
onCreate() 在启动时实现这个以初始化您的内容提供者。 |
AssetFileDescriptor |
openTypedAssetFile(Uri url, String mimeType, Bundle opts) 由客户端调用以打开包含特定MIME类型数据的只读流。 |
Cursor |
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 实现这个来处理来自客户端的查询请求。 |
int |
update(Uri uri, ContentValues values, String selection, String[] selectionArgs) 实现这个来处理更新一行或多行的请求。 |
继承方法(Inherited methods) |
|
|---|---|
android.content.ContentProvider
|
|
java.lang.Object
|
|
android.content.ComponentCallbacks2
|
|
android.content.ComponentCallbacks
|
|
MockContentProvider (Context context)
接受Context实例的构造函数,该实例应该是 MockContext的子类。
| 参数(Parameters) | |
|---|---|
context |
Context
|
MockContentProvider (Context context, String readPermission, String writePermission, PathPermission[] pathPermissions)
初始化内部具有 ContentProvider四个成员变量的构造函数。
| 参数(Parameters) | |
|---|---|
context |
Context: A Context object which should be some mock instance (like the instance of MockContext). |
readPermission |
String: The read permision you want this instance should have in the test, which is available via getReadPermission(). |
writePermission |
String: The write permission you want this instance should have in the test, which is available via getWritePermission(). |
pathPermissions |
PathPermission: The PathPermissions you want this instance should have in the test, which is available via getPathPermissions(). |
ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations)
重写此操作以处理执行一批操作的请求,或者默认实现将迭代操作并在每个操作上调用apply(ContentProvider, ContentProviderResult[], int) 。 如果所有对apply(ContentProvider, ContentProviderResult[], int)调用apply(ContentProvider, ContentProviderResult[], int)成功,则会返回一个ContentProviderResult数组,其元素数与操作数一样多。 如果有任何呼叫失败,则实施有多少个生效。 该方法可以从多个线程中调用,如Processes and Threads中所述 。
| 参数(Parameters) | |
|---|---|
operations |
ArrayList: the operations to apply |
| 返回(Returns) | |
|---|---|
ContentProviderResult[] |
the results of the applications |
void attachInfo (Context context, ProviderInfo info)
在被实例化之后,这被称为告诉内容提供者自己。
| 参数(Parameters) | |
|---|---|
context |
Context: The context this provider is running in |
info |
ProviderInfo: Registered information about this content provider |
int bulkInsert (Uri uri, ContentValues[] values)
如果你不愿意手动实现,请调用super.bulkInsert()。
| 参数(Parameters) | |
|---|---|
uri |
Uri: The content:// URI of the insertion request. |
values |
ContentValues: An array of sets of column_name/value pairs to add to the database. This must not be null. |
| 返回(Returns) | |
|---|---|
int |
The number of values that were inserted. |
int delete (Uri uri, String selection, String[] selectionArgs)
实现这个来处理删除一行或多行的请求。 执行应该在执行删除时应用选择子句,从而允许操作影响目录中的多个行。 作为礼节,删除后请致电notifyChange() 。 该方法可以从多个线程中调用,如Processes and Threads中所述 。
如果特定的行被删除,该实现负责解析URI末尾的行ID。 也就是说,客户端将传入content://contacts/people/22 ,并且实现负责在创建SQL语句时解析记录号(22)。
| 参数(Parameters) | |
|---|---|
uri |
Uri: The full URI to query, including a row ID (if a specific record is requested). |
selection |
String: An optional restriction to apply to rows when deleting. |
selectionArgs |
String
|
| 返回(Returns) | |
|---|---|
int |
The number of rows affected. |
String[] getStreamTypes (Uri url, String mimeTypeFilter)
由客户端调用以确定此内容提供程序支持的给定URI的数据流类型。 默认实现返回null ,表示没有类型。 如果您的内容提供者存储特定类型的数据,则返回该MIME类型(如果它与给定的mimeTypeFilter匹配)。 如果它可以执行类型转换,则返回与mimeTypeFilter匹配的所有支持的MIME类型的数组。
| 参数(Parameters) | |
|---|---|
url |
Uri: The data in the content provider being queried. |
mimeTypeFilter |
String: The type of data the client desires. May be a pattern, such as */* to retrieve all possible data types. |
| 返回(Returns) | |
|---|---|
String[] |
Returns null if there are no possible data streams for the given mimeTypeFilter. Otherwise returns an array of all available concrete MIME types. |
String getType (Uri uri)
实现这个来处理给定URI处的数据的MIME类型请求。 返回的MIME类型应该以vnd.android.cursor.item为单个记录vnd.android.cursor.dir/ ,或者vnd.android.cursor.dir/为多个项目vnd.android.cursor.dir/ 。 该方法可以从多个线程中调用,如Processes and Threads中所述 。
请注意,应用程序访问此信息不需要任何权限; 如果您的内容提供者需要读取和/或写入权限,或者未导出,则所有应用程序仍可以调用此方法,而不考虑其访问权限。 这允许他们在调度意图时检索URI的MIME类型。
| 参数(Parameters) | |
|---|---|
uri |
Uri: the URI to query. |
| 返回(Returns) | |
|---|---|
String |
a MIME type string, or null if there is no type. |
Uri insert (Uri uri, ContentValues values)
实现这个来处理插入新行的请求。 作为礼节,插入后请拨打notifyChange() 。 该方法可以从多个线程中调用,如Processes and Threads中所述 。
| 参数(Parameters) | |
|---|---|
uri |
Uri: The content:// URI of the insertion request. This must not be null. |
values |
ContentValues: A set of column_name/value pairs to add to the database. This must not be null. |
| 返回(Returns) | |
|---|---|
Uri |
The URI for the newly inserted item. |
boolean onCreate ()
在启动时实现这个以初始化您的内容提供者。 在应用程序启动时,应用程序主线程上的所有注册内容提供者都会调用此方法。 它不能执行冗长的操作,否则应用程序启动将会延迟。
你应该推迟平凡的初始化(如打开,升级和扫描数据库),直到使用内容提供商(通过query(Uri, String[], String, String[], String) , insert(Uri, ContentValues) ,等等)。 延迟初始化可以使应用程序快速启动,避免不必要的工作(如果提供程序不需要),并停止数据库错误(如完整磁盘)停止应用程序启动。
如果您使用SQLite, SQLiteOpenHelper是一个有用的实用程序类,可以轻松管理数据库,并且会在首次使用前自动延迟打开。 如果您确实使用SQLiteOpenHelper,请确保避免使用此方法调用getReadableDatabase()或getWritableDatabase() 。 (相反,覆盖onOpen(SQLiteDatabase)以便在数据库第一次打开时对其进行初始化。)
| 返回(Returns) | |
|---|---|
boolean |
true if the provider was successfully loaded, false otherwise |
AssetFileDescriptor openTypedAssetFile (Uri url, String mimeType, Bundle opts)
由客户端调用以打开包含特定MIME类型数据的只读流。 这就像openAssetFile(Uri, String) ,除了文件只能是只读的,内容提供者可以执行数据转换来生成所需类型的数据。
默认实现将给定的mimeType与 getType(Uri)的结果进行 getType(Uri) ,如果它们匹配,则简单地调用 openAssetFile(Uri, String) 。
有关此方法的使用和实施示例,请参见 ClipData 。
返回的AssetFileDescriptor可以是管道或套接字对,以启用数据流式传输。
为了更好地与其他应用程序互操作,建议对于可以打开的任何URI,还支持对其至少包含由OpenableColumns指定的列的OpenableColumns 。 如果您需要提供其他元数据,则可能还需要支持其他常用列,例如DATE_ADDED中的MediaStore.MediaColumns 。
| 参数(Parameters) | |
|---|---|
url |
Uri: The data in the content provider being queried. |
mimeType |
String: The type of data the client desires. May be a pattern, such as */*, if the caller does not have specific type requirements; in this case the content provider will pick its best type matching the pattern. |
opts |
Bundle: Additional options from the client. The definitions of these are specific to the content provider being called. |
| 返回(Returns) | |
|---|---|
AssetFileDescriptor |
Returns a new AssetFileDescriptor from which the client can read data of the desired type. |
Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
实现这个来处理来自客户端的查询请求。 该方法可以从多个线程调用,如Processes and Threads中所述 。
客户呼叫示例:
// Request a specific record.
Cursor managedCursor = managedQuery(
ContentUris.withAppendedId(Contacts.People.CONTENT_URI, 2),
projection, // Which columns to return.
null, // WHERE clause.
null, // WHERE clause value substitution
People.NAME + " ASC"); // Sort order. Example implementation:
// SQLiteQueryBuilder is a helper class that creates the
// proper SQL syntax for us.
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
// Set the table we're querying.
qBuilder.setTables(DATABASE_TABLE_NAME);
// If the query ends in a specific record number, we're
// being asked for a specific record, so set the
// WHERE clause in our query.
if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
qBuilder.appendWhere("_id=" + uri.getPathLeafId());
}
// Make the query.
Cursor c = qBuilder.query(mDb,
projection,
selection,
selectionArgs,
groupBy,
having,
sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
| 参数(Parameters) | |
|---|---|
uri |
Uri: The URI to query. This will be the full URI sent by the client; if the client is requesting a specific record, the URI will end in a record number that the implementation should parse and add to a WHERE or HAVING clause, specifying that _id value. |
projection |
String: The list of columns to put into the cursor. If null all columns are included. |
selection |
String: A selection criteria to apply when filtering rows. If null then all rows are included. |
selectionArgs |
String: You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. |
sortOrder |
String: How the rows in the cursor should be sorted. If null then the provider is free to define the sort order. |
| 返回(Returns) | |
|---|---|
Cursor |
a Cursor or null. |
int update (Uri uri, ContentValues values, String selection, String[] selectionArgs)
实现这个来处理更新一行或多行的请求。 实现应该更新与选择相匹配的所有行,以根据提供的值映射设置列。 为了礼貌,更新后请致电notifyChange() 。 该方法可以从多个线程中调用,如Processes and Threads中所述 。
| 参数(Parameters) | |
|---|---|
uri |
Uri: The URI to query. This can potentially have a record ID if this is an update request for a specific record. |
values |
ContentValues: A set of column_name/value pairs to update in the database. This must not be null. |
selection |
String: An optional filter to match rows to update. |
selectionArgs |
String
|
| 返回(Returns) | |
|---|---|
int |
the number of rows affected. |