public class Instrumentation
extends Object
| java.lang.Object | |
| android.app.Instrumentation | |
| |
| |
用于实现应用程序检测代码的基类。 当开启仪器运行时,该类将在任何应用程序代码之前为您实例化,从而允许您监视系统与应用程序之间的所有交互。 Instrumentation实现通过AndroidManifest.xml的<instrumentation>标签向系统描述。
Nested classes |
|
|---|---|
class |
Instrumentation.ActivityMonitor 有关正在监控的特定种类Intent的信息。 |
class |
Instrumentation.ActivityResult 要返回到原始活动的活动执行结果的描述。 |
常量(Constants) |
|
|---|---|
String |
REPORT_KEY_IDENTIFIER 如果包含在发送到IInstrumentationWatcher的状态或最终包中,则此键标识正在编写报告的类。 |
String |
REPORT_KEY_STREAMRESULT 如果包含在发送到IInstrumentationWatcher的状态或最终捆绑包中,则此键标识可以简单地打印到输出流的字符串。 |
Public constructors |
|
|---|---|
Instrumentation() |
|
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
String REPORT_KEY_IDENTIFIER
如果包含在发送到IInstrumentationWatcher的状态或最终包中,则此键标识正在编写报告的类。 这可用于在IInstrumentationWatcher中提供更多结构化的日志记录或报告功能。
常量值:“id”
String REPORT_KEY_STREAMRESULT
如果包含在发送到IInstrumentationWatcher的状态或最终捆绑包中,则此键标识可以简单地打印到输出流的字符串。 使用这些流提供了状态和最终数据包的“漂亮打印机”版本。 任何捆绑包括这个密钥也应该包括一套完整的原始键/值对,这样也可以启动仪器,并且通过自动化系统收集结果。
常数值:“流”
Instrumentation.ActivityMonitor addMonitor (String cls, Instrumentation.ActivityResult result, boolean block)
addMonitor(ActivityMonitor)的便利包装,为您创建一个匹配 Instrumentation.ActivityMonitor的类并将其返回。
| 参数(Parameters) | |
|---|---|
cls |
String: The activity class this monitor is responsible for. |
result |
Instrumentation.ActivityResult: A canned result to return if the monitor is hit; can be null. |
block |
boolean: Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. |
| 返回(Returns) | |
|---|---|
Instrumentation.ActivityMonitor |
The newly created and added activity monitor. |
Instrumentation.ActivityMonitor addMonitor (IntentFilter filter, Instrumentation.ActivityResult result, boolean block)
addMonitor(ActivityMonitor)的便利包装,为您创建一个匹配 Instrumentation.ActivityMonitor的意向过滤器并将其返回。
| 参数(Parameters) | |
|---|---|
filter |
IntentFilter: The set of intents this monitor is responsible for. |
result |
Instrumentation.ActivityResult: A canned result to return if the monitor is hit; can be null. |
block |
boolean: Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. |
| 返回(Returns) | |
|---|---|
Instrumentation.ActivityMonitor |
The newly created and added activity monitor. |
void addMonitor (Instrumentation.ActivityMonitor monitor)
添加一个新的Instrumentation.ActivityMonitor ,每当一个活动开始时将被检查。 显示器在任何现有的显示器之后添加; 只有当现有显示器都不能处理意图时,才会触发显示器。
| 参数(Parameters) | |
|---|---|
monitor |
Instrumentation.ActivityMonitor: The new ActivityMonitor to see. |
void callActivityOnCreate (Activity activity, Bundle icicle)
执行一个活动的onCreate(Bundle)方法的调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being created. |
icicle |
Bundle: The previously frozen state (or null) to pass through to onCreate(). |
void callActivityOnCreate (Activity activity, Bundle icicle, PersistableBundle persistentState)
执行一个活动的方法onCreate(Bundle)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being created. |
icicle |
Bundle: The previously frozen state (or null) to pass through to |
persistentState |
PersistableBundle: The previously persisted state (or null) |
void callActivityOnDestroy (Activity activity)
| 参数(Parameters) | |
|---|---|
activity |
Activity
|
void callActivityOnNewIntent (Activity activity, Intent intent)
执行一个活动的方法onNewIntent(Intent)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity receiving a new Intent. |
intent |
Intent: The new intent being received. |
void callActivityOnPause (Activity activity)
执行一个活动的方法onPause()调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being paused. |
void callActivityOnPostCreate (Activity activity, Bundle icicle, PersistableBundle persistentState)
执行一个活动的方法onPostCreate(Bundle)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being created. |
icicle |
Bundle: The previously frozen state (or null) to pass through to onPostCreate(). |
persistentState |
PersistableBundle
|
void callActivityOnPostCreate (Activity activity, Bundle icicle)
执行一个活动的方法onPostCreate(Bundle)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being created. |
icicle |
Bundle: The previously frozen state (or null) to pass through to onPostCreate(). |
void callActivityOnRestart (Activity activity)
执行一项活动的方法onRestart() 。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being restarted. |
void callActivityOnRestoreInstanceState (Activity activity, Bundle savedInstanceState)
执行调用一个活动的的onRestoreInstanceState(Bundle)方法。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being restored. |
savedInstanceState |
Bundle: The previously saved state being restored. |
void callActivityOnRestoreInstanceState (Activity activity, Bundle savedInstanceState, PersistableBundle persistentState)
执行一个活动的方法onRestoreInstanceState(Bundle)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being restored. |
savedInstanceState |
Bundle: The previously saved state being restored. |
persistentState |
PersistableBundle: The previously persisted state (or null) |
void callActivityOnResume (Activity activity)
执行一个活动的方法onResume()调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being resumed. |
void callActivityOnSaveInstanceState (Activity activity, Bundle outState, PersistableBundle outPersistentState)
执行一个活动的方法onSaveInstanceState(Bundle)调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being saved. |
outState |
Bundle: The bundle to pass to the call. |
outPersistentState |
PersistableBundle: The persistent bundle to pass to the call. |
void callActivityOnSaveInstanceState (Activity activity, Bundle outState)
执行调用一个活动的的onSaveInstanceState(Bundle)方法。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being saved. |
outState |
Bundle: The bundle to pass to the call. |
void callActivityOnStart (Activity activity)
执行一个活动的onStart()方法的调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being started. |
void callActivityOnStop (Activity activity)
执行一个活动的onStop()方法的调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being stopped. |
void callActivityOnUserLeaving (Activity activity)
执行一个活动的方法onUserLeaveHint()调用。 默认实现只需调用该方法即可。
| 参数(Parameters) | |
|---|---|
activity |
Activity: The activity being notified that the user has navigated away |
void callApplicationOnCreate (Application app)
执行应用程序的onCreate()方法的调用。 默认实现只需调用该方法即可。
注意:此方法将在onCreate(Bundle)之后立即onCreate(Bundle) 。 仪器测试经常在onCreate()中开始他们的测试线程; 你需要小心这些之间的比赛。 (嗯,它和其他一切,但我们从这里开始。)
| 参数(Parameters) | |
|---|---|
app |
Application: The application being created. |
boolean checkMonitorHit (Instrumentation.ActivityMonitor monitor, int minHits)
测试现有的Instrumentation.ActivityMonitor是否已被击中。 如果监视器至少被击中了minHits次,那么它将从活动监视器列表中移除并且返回true。 否则它保持原样并返回false。
| 参数(Parameters) | |
|---|---|
monitor |
Instrumentation.ActivityMonitor: The ActivityMonitor to check. |
minHits |
int: The minimum number of hits required. |
| 返回(Returns) | |
|---|---|
boolean |
True if the hit count has been reached, else false. |
void finish (int resultCode,
Bundle results)
终止应用程序的检测。 这将导致应用程序进程退出,并在下次启动应用程序时删除此检测。
| 参数(Parameters) | |
|---|---|
resultCode |
int: Overall success/failure of instrumentation. |
results |
Bundle: Any results to send back to the code that started the instrumentation. |
Bundle getBinderCounts ()
返回包含此过程的各种绑定程序计数的绑定。 目前仅报告的两个是发送的数量和收到的事务数量。
| 返回(Returns) | |
|---|---|
Bundle |
|
ComponentName getComponentName ()
返回此工具的完整组件名称。
| 返回(Returns) | |
|---|---|
ComponentName |
Returns the complete component name for this instrumentation. |
Context getContext ()
返回这个工具包的上下文。 请注意,这通常与被测试的应用程序的上下文不同,因为测试代码通常与其运行的应用程序不同。 请参阅getTargetContext()以检索目标应用程序的上下文。
| 返回(Returns) | |
|---|---|
Context |
The instrumentation's package context. |
也可以看看:
Context getTargetContext ()
为正在检测的目标应用程序返回一个上下文。 请注意,这通常与检测代码的上下文不同,因为检测代码通常与其运行的应用程序不同。 请参阅getContext()以获取检测代码的上下文。
| 返回(Returns) | |
|---|---|
Context |
A Context in the target application. |
也可以看看:
UiAutomation getUiAutomation (int flags)
获取设置了标志的 UiAutomation实例。
注意:通过返回的UiAutomation公开的APIs跨越应用程序边界工作,而由仪器公开的API则没有。 例如, sendPointerSync(MotionEvent)将不允许您将事件注入与检测目标不同的应用程序,而injectInputEvent(android.view.InputEvent, boolean)将工作,而不管当前的应用程序如何。
典型的测试用例应该使用UiAutomation或Instrumentation API。 同时使用这两种API本身并不是一个错误,但客户必须了解这些API的局限性。
如果 UiAutomation存在不同的标志,则该实例上的标志将被更改,然后它将被返回。
| 参数(Parameters) | |
|---|---|
flags |
int: The flags to be passed to the UiAutomation, for example FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES. |
| 返回(Returns) | |
|---|---|
UiAutomation |
The UI automation instance. |
也可以看看:
UiAutomation getUiAutomation ()
获取没有设置标志的 UiAutomation实例。
注意:通过返回的UiAutomation公开的API可以跨应用程序边界工作,而由仪器公开的API不会。 例如, sendPointerSync(MotionEvent)将不允许您将事件注入到与检测目标不同的应用程序中,而injectInputEvent(android.view.InputEvent, boolean)将工作,而不管当前的应用程序如何。
典型的测试用例应该使用UiAutomation或Instrumentation API。 同时使用这两种API本身并不是一个错误,但客户必须了解这些API的局限性。
相当于getUiAutomation(0) 。 如果UiAutomation存在不同的标志,那么该实例上的标志将被更改,然后它将被返回。
| 返回(Returns) | |
|---|---|
UiAutomation |
The UI automation instance. |
也可以看看:
boolean invokeContextMenuAction (Activity targetActivity, int id, int flag)
显示当前焦点视图的上下文菜单并执行特定的上下文菜单项。
| 参数(Parameters) | |
|---|---|
targetActivity |
Activity: The activity in question. |
id |
int: The identifier associated with the context menu item. |
flag |
int: Additional flags, if any. |
| 返回(Returns) | |
|---|---|
boolean |
Whether the invocation was successful (for example, it could be false if item is disabled). |
boolean invokeMenuActionSync (Activity targetActivity, int id, int flag)
执行特定的菜单项。
| 参数(Parameters) | |
|---|---|
targetActivity |
Activity: The activity in question. |
id |
int: The identifier associated with the menu item. |
flag |
int: Additional flags, if any. |
| 返回(Returns) | |
|---|---|
boolean |
Whether the invocation was successful (for example, it could be false if item is disabled). |
boolean isProfiling ()
检查此检测是否启用了分析启用。
| 返回(Returns) | |
|---|---|
boolean |
Returns true if profiling was enabled when starting, else false. |
Activity newActivity (Class<?> clazz, Context context, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance)
执行Activity对象的实例化。 此方法旨在用于单元测试,例如android.test.ActivityUnitTestCase。 该活动将在本地使用,但会遗漏系统内部使用的一些联系。
| 参数(Parameters) | |
|---|---|
clazz |
Class: The Class of the desired Activity |
context |
Context: The base context for the activity to use |
token |
IBinder: The token for this activity to communicate with |
application |
Application: The application object (if any) |
intent |
Intent: The intent that started this Activity |
info |
ActivityInfo: ActivityInfo from the manifest |
title |
CharSequence: The title, typically retrieved from the ActivityInfo record |
parent |
Activity: The parent Activity (if any) |
id |
String: The embedded Id (if any) |
lastNonConfigurationInstance |
Object: Arbitrary object that will be available via Activity.getLastNonConfigurationInstance(). |
| 返回(Returns) | |
|---|---|
Activity |
Returns the instantiated activity |
| 抛出异常(Throws) | |
|---|---|
|
InstantiationException |
IllegalAccessException |
|
InstantiationException |
|
Activity newActivity (ClassLoader cl, String className, Intent intent)
执行进程的Activity对象的实例化。 默认实现提供了正常的系统行为。
| 参数(Parameters) | |
|---|---|
cl |
ClassLoader: The ClassLoader with which to instantiate the object. |
className |
String: The name of the class implementing the Activity object. |
intent |
Intent: The Intent object that specified the activity class being instantiated. |
| 返回(Returns) | |
|---|---|
Activity |
The newly instantiated Activity object. |
| 抛出异常(Throws) | |
|---|---|
InstantiationException |
|
IllegalAccessException |
|
ClassNotFoundException |
|
Application newApplication (ClassLoader cl, String className, Context context)
执行进程的Application对象的实例化。 默认实现提供了正常的系统行为。
| 参数(Parameters) | |
|---|---|
cl |
ClassLoader: The ClassLoader with which to instantiate the object. |
className |
String: The name of the class implementing the Application object. |
context |
Context: The context to initialize the application with |
| 返回(Returns) | |
|---|---|
Application |
The newly instantiated Application object. |
| 抛出异常(Throws) | |
|---|---|
InstantiationException |
|
IllegalAccessException |
|
ClassNotFoundException |
|
Application newApplication (Class<?> clazz, Context context)
执行进程的Application对象的实例化。 默认实现提供了正常的系统行为。
| 参数(Parameters) | |
|---|---|
clazz |
Class: The class used to create an Application object from. |
context |
Context: The context to initialize the application with |
| 返回(Returns) | |
|---|---|
Application |
The newly instantiated Application object. |
| 抛出异常(Throws) | |
|---|---|
InstantiationException |
|
IllegalAccessException |
|
ClassNotFoundException |
|
void onCreate (Bundle arguments)
在仪器启动时调用,在加载任何应用程序代码之前。 通常这将被实现为只需调用start()来开始检测线程,然后将继续执行onStart() 。
如果你不需要你自己的线程 - 那就是你正在编写你的工具是完全异步的(返回到事件循环以便应用程序可以运行),那么你可以简单地在这里开始你的工具,例如调用 startActivity(Intent)开始适当的第一个应用程序的活动。
| 参数(Parameters) | |
|---|---|
arguments |
Bundle: Any additional arguments that were supplied when the instrumentation was started. |
boolean onException (Object obj, Throwable e)
只要系统捕获应用程序抛出的未处理的异常,就会调用它。 默认实现只返回false,允许正常的系统处理异常发生。
| 参数(Parameters) | |
|---|---|
obj |
Object: The client object that generated the exception. May be an Application, Activity, BroadcastReceiver, Service, or null. |
e |
Throwable: The exception that was thrown. |
| 返回(Returns) | |
|---|---|
boolean |
To allow normal system exception process to occur, return false. If true is returned, the system will proceed as if the exception didn't happen. |
void onStart ()
检测线程进入执行的方法。 这使您可以在与应用程序不同的线程中运行检测代码,以便它可以执行阻止操作,例如sendKeySync(KeyEvent)或startActivitySync(Intent) 。
当这个函数完成时,你通常会想调用finish()来结束你的检测。
void removeMonitor (Instrumentation.ActivityMonitor monitor)
删除 Instrumentation.ActivityMonitor以前添加了 addMonitor(Instrumentation.ActivityMonitor) 。
| 参数(Parameters) | |
|---|---|
monitor |
Instrumentation.ActivityMonitor: The monitor to remove. |
void runOnMainSync (Runnable runner)
在应用程序的主线程上执行调用,直到完成为止。 用于处理非线程安全的事情,例如查看或修改视图层次结构。
| 参数(Parameters) | |
|---|---|
runner |
Runnable: The code to run on the main thread. |
void sendCharacterSync (int keyCode)
用于发送特定字符键码的向下和向上键事件的更高级别的方法。 相当于手工创建两个KeyEvent对象并调用sendKeySync(KeyEvent) 。 该事件看起来好像来自键盘0,内置一个。
| 参数(Parameters) | |
|---|---|
keyCode |
int: The key code of the character to send. |
void sendKeyDownUpSync (int key)
将向上和向下键事件同步发送到当前聚焦的窗口。
| 参数(Parameters) | |
|---|---|
key |
int: The integer keycode for the event. |
void sendKeySync (KeyEvent event)
将关键事件发送到当前聚焦的窗口/视图并等待它被处理。 在收件人从事件处理返回后的某一时刻完成,尽管它可能没有完全结束事件的反应 - 例如,如果它需要更新其显示的结果,它可能仍然处于正在处理的过程中那。
| 参数(Parameters) | |
|---|---|
event |
KeyEvent: The event to send to the current focus. |
void sendPointerSync (MotionEvent event)
分派指针事件。 在收件人从事件处理返回后的某一时刻完成,尽管它可能没有完全结束事件的反应 - 例如,如果它需要更新其显示的结果,它可能仍然处于正在处理的过程中那。
| 参数(Parameters) | |
|---|---|
event |
MotionEvent: A motion event describing the pointer action. (As noted in obtain(long, long, int, float, float, int), be sure to use uptimeMillis() as the timebase. |
void sendStatus (int resultCode,
Bundle results)
提供关于应用程序的状态报告。
| 参数(Parameters) | |
|---|---|
resultCode |
int: Current success/failure of instrumentation. |
results |
Bundle: Any results to send back to the code that started the instrumentation. |
void sendStringSync (String text)
将与文本相对应的关键事件发送到正在进行检测的应用程序。
| 参数(Parameters) | |
|---|---|
text |
String: The text to be sent. |
void sendTrackballEventSync (MotionEvent event)
发送跟踪球事件。 在收件人从事件处理返回后的某一时刻完成,尽管它可能没有完全结束事件的反应 - 例如,如果它需要更新其显示的结果,它可能仍然处于正在处理的过程中那。
| 参数(Parameters) | |
|---|---|
event |
MotionEvent: A motion event describing the trackball action. (As noted in obtain(long, long, int, float, float, int), be sure to use uptimeMillis() as the timebase. |
void setInTouchMode (boolean inTouch)
强制全局系统进入或退出触摸模式。 如果您的工具在启动时依赖于UI,则可以使用此功能。
| 参数(Parameters) | |
|---|---|
inTouch |
boolean: Set to true to be in touch mode, false to be in focus mode. |
Activity startActivitySync (Intent intent)
开始一项新活动并等待它在返回之前开始运行。 除了同步之外,此方法与标准startActivity(Intent)调用之间存在某些语义差异:活动组件在与活动管理器交谈之前解析(其类名在Intent中指定,此方法最终开始),并且它不允许你开始在不同的过程中运行的活动。 另外,如果给定的Intent解析为多个活动,而不是显示用户选择活动的对话框,则会抛出异常。
一旦活动在对onCreate(Bundle)的调用之后变空闲,函数就会返回。 通常这意味着它已经完成了全面的初始化,包括onResume()并绘制并显示了它的初始窗口。
| 参数(Parameters) | |
|---|---|
intent |
Intent: Description of the activity to start. |
| 返回(Returns) | |
|---|---|
Activity |
|
也可以看看:
void startProfiling ()
如果isProfiling()返回true,则此方法将开始分析。 如果将此Instrumentation的清单文件中的handleProfiling属性设置为true,则只应调用此方法。
void waitForIdle (Runnable recipient)
为应用程序的主线程闲置时安排回调(不再有要处理的事件)。
| 参数(Parameters) | |
|---|---|
recipient |
Runnable: Called the next time the thread's message queue is idle. |
void waitForIdleSync ()
同步等待应用程序闲置。 无法从主应用程序线程调用 - 使用start()在其自己的线程中执行检测。
Activity waitForMonitor (Instrumentation.ActivityMonitor monitor)
等待现有的Instrumentation.ActivityMonitor被击中。 一旦监视器被击中,它将从活动监视器列表中移除,并返回与之匹配的第一个创建的Activity对象。
| 参数(Parameters) | |
|---|---|
monitor |
Instrumentation.ActivityMonitor: The ActivityMonitor to wait for. |
| 返回(Returns) | |
|---|---|
Activity |
The Activity object that matched the monitor. |
Activity waitForMonitorWithTimeout (Instrumentation.ActivityMonitor monitor, long timeOut)
等待现有的Instrumentation.ActivityMonitor被击中,直到超时到期。 一旦监视器被击中,它将从活动监视器列表中移除,并返回与之匹配的第一个创建的Activity对象。 如果超时过期,则返回空对象。
| 参数(Parameters) | |
|---|---|
monitor |
Instrumentation.ActivityMonitor: The ActivityMonitor to wait for. |
timeOut |
long: The timeout value in secs. |
| 返回(Returns) | |
|---|---|
Activity |
The Activity object that matched the monitor. |