public boolean invokeMenuActionSync(Activity targetActivity, int id, int flag)

Added in API level 1

Execute a particular menu item.

Parameters
targetActivityThe activity in question.
idThe identifier associated with the menu item.
flagAdditional flags, if any.
Returns
  • Whether the invocation was successful (for example, it could be false if item is disabled).

public boolean isProfiling()

Added in API level 1

Check whether this instrumentation was started with profiling enabled.

Returns
  • Returns true if profiling was enabled when starting, else false.

public ActivitynewActivity(ClassLoader cl, String className, Intent intent)

Added in API level 1

Perform instantiation of the process's Activity object. The default implementation provides the normal system behavior.

Parameters
clThe ClassLoader with which to instantiate the object.
classNameThe name of the class implementing the Activity object.
intentThe Intent object that specified the activity class being instantiated.
Returns
  • The newly instantiated Activity object.

public ActivitynewActivity(Class<?> clazz, Context context, IBinder token, Application application, Intent intent,ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance)

Added in API level 1

Perform instantiation of an Activity object. This method is intended for use with unit tests, such as android.test.ActivityUnitTestCase. The activity will be useable locally but will be missing some of the linkages necessary for use within the sytem.

Parameters
clazzThe Class of the desired Activity
contextThe base context for the activity to use
tokenThe token for this activity to communicate with
applicationThe application object (if any)
intentThe intent that started this Activity
infoActivityInfo from the manifest
titleThe title, typically retrieved from the ActivityInfo record
parentThe parent Activity (if any)
idThe embedded Id (if any)
lastNonConfigurationInstanceArbitrary object that will be available via Activity.getLastNonConfigurationInstance().
Returns
  • Returns the instantiated activity

public ApplicationnewApplication(ClassLoader cl, String className, Context context)

Added in API level 1

Perform instantiation of the process's Application object. The default implementation provides the normal system behavior.

Parameters
clThe ClassLoader with which to instantiate the object.
classNameThe name of the class implementing the Application object.
contextThe context to initialize the application with
Returns
  • The newly instantiated Application object.

public static ApplicationnewApplication(Class<?> clazz, Context context)

Added in API level 1

Perform instantiation of the process's Application object. The default implementation provides the normal system behavior.

Parameters
clazzThe class used to create an Application object from.
contextThe context to initialize the application with
Returns
  • The newly instantiated Application object.

public void onCreate(Bundle arguments)

Added in API level 1

Called when the instrumentation is starting, before any application code has been loaded. Usually this will be implemented to simply call start() to begin the instrumentation thread, which will then continue execution in onStart().

If you do not need your own thread -- that is you are writing your instrumentation to be completely asynchronous (returning to the event loop so that the application can run), you can simply begin your instrumentation here, for example call startActivity(Intent) to begin the appropriate first activity of the application.

Parameters
argumentsAny additional arguments that were supplied when the instrumentation was started.

public void onDestroy()

Added in API level 1

Called when the instrumented application is stopping, after all of the normal application cleanup has occurred.

public boolean onException(Object obj, Throwable e)

Added in API level 1

This is called whenever the system captures an unhandled exception that was thrown by the application. The default implementation simply returns false, allowing normal system handling of the exception to take place.

Parameters
objThe client object that generated the exception. May be an Application, Activity, BroadcastReceiver, Service, or null.
eThe exception that was thrown.
Returns
  • 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.

public void onStart()

Added in API level 1

Method where the instrumentation thread enters execution. This allows you to run your instrumentation code in a separate thread than the application, so that it can perform blocking operation such as sendKeySync(KeyEvent) or startActivitySync(Intent).

You will typically want to call finish() when this function is done, to end your instrumentation.

public void removeMonitor(Instrumentation.ActivityMonitor monitor)

Added in API level 1
Parameters
monitorThe monitor to remove.

public void runOnMainSync(Runnable runner)

Added in API level 1

Execute a call on the application's main thread, blocking until it is complete. Useful for doing things that are not thread-safe, such as looking at or modifying the view hierarchy.

Parameters
runnerThe code to run on the main thread.

public void sendCharacterSync(int keyCode)

Added in API level 1

Higher-level method for sending both the down and up key events for a particular character key code. Equivalent to creating both KeyEvent objects by hand and calling sendKeySync(KeyEvent). The event appears as if it came from keyboard 0, the built in one.

Parameters
keyCodeThe key code of the character to send.

public void sendKeyDownUpSync(int key)

Added in API level 1

Sends an up and down key event sync to the currently focused window.

Parameters
keyThe integer keycode for the event.

public void sendKeySync(KeyEvent event)

Added in API level 1

Send a key event to the currently focused window/view and wait for it to be processed. Finished at some point after the recipient has returned from its event processing, though it may not have completely finished reacting from the event -- for example, if it needs to update its display as a result, it may still be in the process of doing that.

Parameters
eventThe event to send to the current focus.

public void sendPointerSync(MotionEvent event)

Added in API level 1

Dispatch a pointer event. Finished at some point after the recipient has returned from its event processing, though it may not have completely finished reacting from the event -- for example, if it needs to update its display as a result, it may still be in the process of doing that.

Parameters
eventA motion event describing the pointer action. (As noted in obtain(long, long, int, float, float, int), be sure to useuptimeMillis() as the timebase.

public void sendStatus(int resultCode, Bundle results)

Added in API level 1

Provide a status report about the application.

Parameters
resultCodeCurrent success/failure of instrumentation.
resultsAny results to send back to the code that started the instrumentation.

public void sendStringSync(String text)

Added in API level 1

Sends the key events corresponding to the text to the app being instrumented.

Parameters
textThe text to be sent.

public void sendTrackballEventSync(MotionEvent event)

Added in API level 1

Dispatch a trackball event. Finished at some point after the recipient has returned from its event processing, though it may not have completely finished reacting from the event -- for example, if it needs to update its display as a result, it may still be in the process of doing that.

Parameters
eventA motion event describing the trackball action. (As noted in obtain(long, long, int, float, float, int), be sure to useuptimeMillis() as the timebase.

public void setAutomaticPerformanceSnapshots()

Added in API level 1


public void setInTouchMode(boolean inTouch)

Added in API level 1

Force the global system in or out of touch mode. This can be used if your instrumentation relies on the UI being in one more or the other when it starts.

Parameters
inTouchSet to true to be in touch mode, false to be in focus mode.

public void start()

Added in API level 1

Create and start a new thread in which to run instrumentation. This new thread will call to onStart() where you can implement the instrumentation.

public ActivitystartActivitySync(Intent intent)

Added in API level 1

Start a new activity and wait for it to begin running before returning. In addition to being synchronous, this method as some semantic differences from the standard startActivity(Intent) call: the activity component is resolved before talking with the activity manager (its class name is specified in the Intent that this method ultimately starts), and it does not allow you to start activities that run in a different process. In addition, if the given Intent resolves to multiple activities, instead of displaying a dialog for the user to select an activity, an exception will be thrown.

The function returns as soon as the activity goes idle following the call to its onCreate(Bundle). Generally this means it has gone through the full initialization including onResume() and drawn and displayed its initial window.

Parameters
intentDescription of the activity to start.

public void startAllocCounting()

Added in API level 1


public void startPerformanceSnapshot()

Added in API level 1


public void startProfiling()

Added in API level 1

This method will start profiling if isProfiling() returns true. You should only call this method if you set the handleProfiling attribute in the manifest file for this Instrumentation to true.

public void stopAllocCounting()

Added in API level 1


public void stopProfiling()

Added in API level 1

Stops profiling if isProfiling() returns true.

public void waitForIdle(Runnable recipient)

Added in API level 1

Schedule a callback for when the application's main thread goes idle (has no more events to process).

Parameters
recipientCalled the next time the thread's message queue is idle.

public void waitForIdleSync()

Added in API level 1

Synchronously wait for the application to be idle. Can not be called from the main application thread -- use start() to execute instrumentation in its own thread.

public ActivitywaitForMonitor(Instrumentation.ActivityMonitor monitor)

Added in API level 1

Wait for an existing Instrumentation.ActivityMonitor to be hit. Once the monitor has been hit, it is removed from the activity monitor list and the first created Activity object that matched it is returned.

Parameters
monitorThe ActivityMonitor to wait for.
Returns
  • The Activity object that matched the monitor.

public ActivitywaitForMonitorWithTimeout(Instrumentation.ActivityMonitor monitor, long timeOut)

Added in API level 1

Wait for an existing Instrumentation.ActivityMonitor to be hit till the timeout expires. Once the monitor has been hit, it is removed from the activity monitor list and the first created Activity object that matched it is returned. If the timeout expires, a null object is returned.

Parameters
monitorThe ActivityMonitor to wait for.
timeOutThe timeout value in secs.
Returns
  • The Activity object that matched the monitor.