IT. Expert System.

Android Reference

Instrumentation


android.app

Class Instrumentation

  • Direct Known Subclasses:
    InstrumentationTestRunner, LaunchPerformanceBase


    public class Instrumentation
    extends Object
    Base class for implementing application instrumentation code. When running with instrumentation turned on, this class will be instantiated for you before any of the application code, allowing you to monitor all of the interaction the system has with the application. An Instrumentation implementation is described to the system through an AndroidManifest.xml's <instrumentation> tag.
    • Field Detail

      • REPORT_KEY_IDENTIFIER

        public static final String REPORT_KEY_IDENTIFIER
        If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the class that is writing the report. This can be used to provide more structured logging or reporting capabilities in the IInstrumentationWatcher.
        See Also:
        Constant Field Values
      • REPORT_KEY_STREAMRESULT

        public static final String REPORT_KEY_STREAMRESULT
        If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies a string which can simply be printed to the output stream. Using these streams provides a "pretty printer" version of the status & final packets. Any bundles including this key should also include the complete set of raw key/value pairs, so that the instrumentation can also be launched, and results collected, by an automated system.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Instrumentation

        public Instrumentation()
    • Method Detail

      • onCreate

        public void onCreate(Bundle arguments)
        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 Context.startActivity(android.content.Intent) to begin the appropriate first activity of the application.

        Parameters:
        arguments - Any additional arguments that were supplied when the instrumentation was started.
      • start

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

        public void onStart()
        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(android.view.KeyEvent) or startActivitySync(android.content.Intent).

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

      • onException

        public boolean onException(Object obj,
                          Throwable e)
        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:
        obj - The client object that generated the exception. May be an Application, Activity, BroadcastReceiver, Service, or null.
        e - The 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.
      • sendStatus

        public void sendStatus(int resultCode,
                      Bundle results)
        Provide a status report about the application.
        Parameters:
        resultCode - Current success/failure of instrumentation.
        results - Any results to send back to the code that started the instrumentation.
      • finish

        public void finish(int resultCode,
                  Bundle results)
        Terminate instrumentation of the application. This will cause the application process to exit, removing this instrumentation from the next time the application is started.
        Parameters:
        resultCode - Overall success/failure of instrumentation.
        results - Any results to send back to the code that started the instrumentation.
      • setAutomaticPerformanceSnapshots

        public void setAutomaticPerformanceSnapshots()
      • startPerformanceSnapshot

        public void startPerformanceSnapshot()
      • endPerformanceSnapshot

        public void endPerformanceSnapshot()
      • onDestroy

        public void onDestroy()
        Called when the instrumented application is stopping, after all of the normal application cleanup has occurred.
      • getContext

        public Context getContext()
        Return the Context of this instrumentation's package. Note that this is often different than the Context of the application being instrumentated, since the instrumentation code often lives is a different package than that of the application it is running against. See getTargetContext() to retrieve a Context for the target application.
        Returns:
        The instrumentation's package context.
        See Also:
        getTargetContext()
      • getComponentName

        public ComponentName getComponentName()
        Returns complete component name of this instrumentation.
        Returns:
        Returns the complete component name for this instrumentation.
      • getTargetContext

        public Context getTargetContext()
        Return a Context for the target application being instrumented. Note that this is often different than the Context of the instrumentation code, since the instrumentation code often lives is a different package than that of the application it is running against. See getContext() to retrieve a Context for the instrumentation code.
        Returns:
        A Context in the target application.
        See Also:
        getContext()
      • isProfiling

        public boolean isProfiling()
        Check whether this instrumentation was started with profiling enabled.
        Returns:
        Returns true if profiling was enabled when starting, else false.
      • startProfiling

        public void startProfiling()
        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.
      • stopProfiling

        public void stopProfiling()
        Stops profiling if isProfiling() returns true.
      • setInTouchMode

        public void setInTouchMode(boolean inTouch)
        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:
        inTouch - Set to true to be in touch mode, false to be in focus mode.
      • waitForIdle

        public void waitForIdle(Runnable recipient)
        Schedule a callback for when the application's main thread goes idle (has no more events to process).
        Parameters:
        recipient - Called the next time the thread's message queue is idle.
      • waitForIdleSync

        public void waitForIdleSync()
        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.
      • runOnMainSync

        public void runOnMainSync(Runnable runner)
        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:
        runner - The code to run on the main thread.
      • startActivitySync

        public Activity startActivitySync(Intent intent)
        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 Context.startActivity(android.content.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 Activity.onCreate(android.os.Bundle). Generally this means it has gone through the full initialization including Activity.onResume() and drawn and displayed its initial window.

        Parameters:
        intent - Description of the activity to start.
        See Also:
        Context.startActivity(android.content.Intent)
      • waitForMonitor

        public Activity waitForMonitor(Instrumentation.ActivityMonitor monitor)
        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:
        monitor - The ActivityMonitor to wait for.
        Returns:
        The Activity object that matched the monitor.
      • waitForMonitorWithTimeout

        public Activity waitForMonitorWithTimeout(Instrumentation.ActivityMonitor monitor,
                                         long timeOut)
        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:
        monitor - The ActivityMonitor to wait for.
        timeOut - The timeout value in secs.
        Returns:
        The Activity object that matched the monitor.
      • invokeMenuActionSync

        public boolean invokeMenuActionSync(Activity targetActivity,
                                   int id,
                                   int flag)
        Execute a particular menu item.
        Parameters:
        targetActivity - The activity in question.
        id - The identifier associated with the menu item.
        flag - Additional flags, if any.
        Returns:
        Whether the invocation was successful (for example, it could be false if item is disabled).
      • invokeContextMenuAction

        public boolean invokeContextMenuAction(Activity targetActivity,
                                      int id,
                                      int flag)
        Show the context menu for the currently focused view and executes a particular context menu item.
        Parameters:
        targetActivity - The activity in question.
        id - The identifier associated with the context menu item.
        flag - Additional flags, if any.
        Returns:
        Whether the invocation was successful (for example, it could be false if item is disabled).
      • sendStringSync

        public void sendStringSync(String text)
        Sends the key events corresponding to the text to the app being instrumented.
        Parameters:
        text - The text to be sent.
      • sendKeySync

        public void sendKeySync(KeyEvent event)
        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:
        event - The event to send to the current focus.
      • sendKeyDownUpSync

        public void sendKeyDownUpSync(int key)
        Sends an up and down key event sync to the currently focused window.
        Parameters:
        key - The integer keycode for the event.
      • sendCharacterSync

        public void sendCharacterSync(int keyCode)
        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(android.view.KeyEvent). The event appears as if it came from keyboard 0, the built in one.
        Parameters:
        keyCode - The key code of the character to send.
      • sendPointerSync

        public void sendPointerSync(MotionEvent event)
        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:
        event - A motion event describing the pointer action. (As noted in MotionEvent.obtain(long, long, int, float, float, int), be sure to use SystemClock.uptimeMillis() as the timebase.
      • sendTrackballEventSync

        public void sendTrackballEventSync(MotionEvent event)
        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:
        event - A motion event describing the trackball action. (As noted in MotionEvent.obtain(long, long, int, float, float, int), be sure to use SystemClock.uptimeMillis() as the timebase.
      • callApplicationOnCreate

        public void callApplicationOnCreate(Application app)
        Perform calling of the application's Application.onCreate() method. The default implementation simply calls through to that method.

        Note: This method will be called immediately after onCreate(Bundle). Often instrumentation tests start their test thread in onCreate(); you need to be careful of races between these. (Well between it and everything else, but let's start here.)

        Parameters:
        app - The application being created.
      • newActivity

        public Activity newActivity(Class<?> clazz,
                           Context context,
                           IBinder token,
                           Application application,
                           Intent intent,
                           ActivityInfo info,
                           CharSequence title,
                           Activity parent,
                           String id,
                           Object lastNonConfigurationInstance)
                             throws InstantiationException,
                                    IllegalAccessException
        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:
        clazz - The Class of the desired Activity
        context - The base context for the activity to use
        token - The token for this activity to communicate with
        application - The application object (if any)
        intent - The intent that started this Activity
        info - ActivityInfo from the manifest
        title - The title, typically retrieved from the ActivityInfo record
        parent - The parent Activity (if any)
        id - The embedded Id (if any)
        lastNonConfigurationInstance - Arbitrary object that will be available via Activity.getLastNonConfigurationInstance().
        Returns:
        Returns the instantiated activity
        Throws:
        InstantiationException
        IllegalAccessException
      • callActivityOnCreate

        public void callActivityOnCreate(Activity activity,
                                Bundle icicle)
        Perform calling of an activity's Activity.onCreate(android.os.Bundle) method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being created.
        icicle - The previously frozen state (or null) to pass through to onCreate().
      • callActivityOnDestroy

        public void callActivityOnDestroy(Activity activity)
      • callActivityOnRestoreInstanceState

        public void callActivityOnRestoreInstanceState(Activity activity,
                                              Bundle savedInstanceState)
        Perform calling of an activity's Activity.onRestoreInstanceState(android.os.Bundle) method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being restored.
        savedInstanceState - The previously saved state being restored.
      • callActivityOnPostCreate

        public void callActivityOnPostCreate(Activity activity,
                                    Bundle icicle)
        Perform calling of an activity's Activity.onPostCreate(android.os.Bundle) method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being created.
        icicle - The previously frozen state (or null) to pass through to onPostCreate().
      • callActivityOnNewIntent

        public void callActivityOnNewIntent(Activity activity,
                                   Intent intent)
        Perform calling of an activity's Activity.onNewIntent(android.content.Intent) method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity receiving a new Intent.
        intent - The new intent being received.
      • callActivityOnStart

        public void callActivityOnStart(Activity activity)
        Perform calling of an activity's Activity.onStart() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being started.
      • callActivityOnRestart

        public void callActivityOnRestart(Activity activity)
        Perform calling of an activity's Activity.onRestart() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being restarted.
      • callActivityOnResume

        public void callActivityOnResume(Activity activity)
        Perform calling of an activity's Activity.onResume() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being resumed.
      • callActivityOnStop

        public void callActivityOnStop(Activity activity)
        Perform calling of an activity's Activity.onStop() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being stopped.
      • callActivityOnSaveInstanceState

        public void callActivityOnSaveInstanceState(Activity activity,
                                           Bundle outState)
        Perform calling of an activity's Activity.onPause() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being saved.
        outState - The bundle to pass to the call.
      • callActivityOnPause

        public void callActivityOnPause(Activity activity)
        Perform calling of an activity's Activity.onPause() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being paused.
      • callActivityOnUserLeaving

        public void callActivityOnUserLeaving(Activity activity)
        Perform calling of an activity's Activity.onUserLeaveHint() method. The default implementation simply calls through to that method.
        Parameters:
        activity - The activity being notified that the user has navigated away
      • startAllocCounting

        public void startAllocCounting()
      • stopAllocCounting

        public void stopAllocCounting()
      • getAllocCounts

        public Bundle getAllocCounts()
        Returns a bundle with the current results from the allocation counting.
      • getBinderCounts

        public Bundle getBinderCounts()
        Returns a bundle with the counts for various binder counts for this process. Currently the only two that are reported are the number of send and the number of received transactions.
      • execStartActivity

        public Instrumentation.ActivityResult execStartActivity(Context who,
                                                       IBinder contextThread,
                                                       IBinder token,
                                                       Activity target,
                                                       Intent intent,
                                                       int requestCode,
                                                       Bundle options)
        Execute a startActivity call made by the application. The default implementation takes care of updating any active Instrumentation.ActivityMonitor objects and dispatches this call to the system activity manager; you can override this to watch for the application to start an activity, and modify what happens when it does.

        This method returns an Instrumentation.ActivityResult object, which you can use when intercepting application calls to avoid performing the start activity action but still return the result the application is expecting. To do this, override this method to catch the call to start activity so that it returns a new ActivityResult containing the results you would like the application to see, and don't call up to the super class. Note that an application is only expecting a result if requestCode is >= 0.

        This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.

        Parameters:
        who - The Context from which the activity is being started.
        contextThread - The main thread of the Context from which the activity is being started.
        token - Internal token identifying to the system who is starting the activity; may be null.
        target - Which activity is performing the start (and thus receiving any result); may be null if this call is not being made from an activity.
        intent - The actual Intent to start.
        requestCode - Identifier for this request's result; less than zero if the caller is not expecting a result.
        options - Addition options.
        Returns:
        To force the return of a particular result, return an ActivityResult object containing the desired data; otherwise return null. The default implementation always returns null.
        Throws:
        ActivityNotFoundException
        See Also:
        Activity.startActivity(Intent), Activity.startActivityForResult(Intent, int), {@hide}
      • execStartActivities

        public void execStartActivities(Context who,
                               IBinder contextThread,
                               IBinder token,
                               Activity target,
                               Intent[] intents,
                               Bundle options)
        Like #execStartActivity(Context, IBinder, IBinder, Activity, Intent, int), but accepts an array of activities to be started. Note that active Instrumentation.ActivityMonitor objects only match against the first activity in the array.
      • execStartActivitiesAsUser

        public void execStartActivitiesAsUser(Context who,
                                     IBinder contextThread,
                                     IBinder token,
                                     Activity target,
                                     Intent[] intents,
                                     Bundle options,
                                     int userId)
        Like #execStartActivity(Context, IBinder, IBinder, Activity, Intent, int), but accepts an array of activities to be started. Note that active Instrumentation.ActivityMonitor objects only match against the first activity in the array.
      • execStartActivity

        public Instrumentation.ActivityResult execStartActivity(Context who,
                                                       IBinder contextThread,
                                                       IBinder token,
                                                       Fragment target,
                                                       Intent intent,
                                                       int requestCode,
                                                       Bundle options)
        Like #execStartActivity(Context, IBinder, IBinder, Activity, Intent, int), but for calls from a {#link Fragment}.
        Parameters:
        who - The Context from which the activity is being started.
        contextThread - The main thread of the Context from which the activity is being started.
        token - Internal token identifying to the system who is starting the activity; may be null.
        target - Which fragment is performing the start (and thus receiving any result).
        intent - The actual Intent to start.
        requestCode - Identifier for this request's result; less than zero if the caller is not expecting a result.
        Returns:
        To force the return of a particular result, return an ActivityResult object containing the desired data; otherwise return null. The default implementation always returns null.
        Throws:
        ActivityNotFoundException
        See Also:
        Activity.startActivity(Intent), Activity.startActivityForResult(Intent, int), {@hide}
      • execStartActivity

        public Instrumentation.ActivityResult execStartActivity(Context who,
                                                       IBinder contextThread,
                                                       IBinder token,
                                                       Activity target,
                                                       Intent intent,
                                                       int requestCode,
                                                       Bundle options,
                                                       UserHandle user)
        Like #execStartActivity(Context, IBinder, IBinder, Activity, Intent, int), but for starting as a particular user.
        Parameters:
        who - The Context from which the activity is being started.
        contextThread - The main thread of the Context from which the activity is being started.
        token - Internal token identifying to the system who is starting the activity; may be null.
        target - Which fragment is performing the start (and thus receiving any result).
        intent - The actual Intent to start.
        requestCode - Identifier for this request's result; less than zero if the caller is not expecting a result.
        Returns:
        To force the return of a particular result, return an ActivityResult object containing the desired data; otherwise return null. The default implementation always returns null.
        Throws:
        ActivityNotFoundException
        See Also:
        Activity.startActivity(Intent), Activity.startActivityForResult(Intent, int), {@hide}


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1535 / 96490034. Delta: 0.34171 с