2012年10月15日 星期一

Android Activity Manager

中文版
Preface

There're four application components in Android. They're Activities, Services, Broadcast receivers, and Content providers.There's only one component has User Interface. That's Activities. Activity Manger can help you to control the application components in Android. Let's take a look how to well use the Activity Manager for our work.

Introduction
There's a shell command called am, which performs the task by Activity Manager. The help of am, you can send the adb command - adb shell am when your computer are connect to your Android device.


usage: am [subcommand] [options]
usage: am start [-D] [-W] [-P ] [--start-profiler ]
               [--R COUNT] [-S] [--opengl-trace]
       am startservice
       am force-stop
       am kill
       am kill-all
       am broadcast
       am instrument [-r] [-e ] [-p ] [-w]
               [--no-window-animation]
       am profile start
       am profile stop []
       am dumpheap [flags]
       am set-debug-app [-w] [--persistent]
       am clear-debug-app
       am monitor [--gdb ]
       am screen-compat [on|off]
       am display-size [reset|MxN]
       am to-uri [INTENT]
       am to-intent-uri [INTENT]

am start: start an Activity.  Options are:
    -D: enable debugging
    -W: wait for launch to complete
    --start-profiler : start profiler and send results to
    -P : like above, but profiling stops when app goes idle
    -R: repeat the activity launch times.  Prior to each repeat,
        the top activity will be finished.
    -S: force stop the target app before starting the activity
    --opengl-trace: enable tracing of OpenGL functions

am startservice: start a Service.

am force-stop: force stop everything associated with .

am kill: Kill all processes associated with .  Only kills.
  processes that are safe to kill -- that is, will not impact the user
  experience.

am kill-all: Kill all background processes.

am broadcast: send a broadcast Intent.

am instrument: start an Instrumentation.  Typically this target
  is the form /.  Options are:
    -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT).  Use with
        [-e perf true] to generate raw output for performance measurements.
    -e : set argument to .  For test runners a
        common form is [-e [,...]].
    -p : write profiling data to
    -w: wait for instrumentation to finish before returning.  Required for
        test runners.
    --no-window-animation: turn off window animations will running.

am profile: start and stop profiler on a process.

am dumpheap: dump the heap of a process.  Options are:
    -n: dump native heap instead of managed heap

am set-debug-app: set application to debug.  Options are:
    -w: wait for debugger when application starts
    --persistent: retain this value

am clear-debug-app: clear the previously set-debug-app.

am monitor: start monitoring for crashes or ANRs.
    --gdb: start gdbserv on the given port at crash/ANR

am screen-compat: control screen compatibility mode of .

am display-size: override display size.

am to-uri: print the given Intent specification as a URI.

am to-intent-uri: print the given Intent specification as an intent: URI.

specifications include these flags and arguments:
    [-a ] [-d ] [-t ]
    [-c [-c ] ...]
    [-e|--es ...]
    [--esn ...]
    [--ez ...]
    [--ei ...]
    [--el ...]
    [--ef ...]
    [--eu ...]
    [--ecn ]
    [--eia [,    [--ela [,    [--efa [,    [-n ] [-f ]
    [--grant-read-uri-permission] [--grant-write-uri-permission]
    [--debug-log-resolution] [--exclude-stopped-packages]
    [--include-stopped-packages]
    [--activity-brought-to-front] [--activity-clear-top]
    [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
    [--activity-launched-from-history] [--activity-multiple-task]
    [--activity-no-animation] [--activity-no-history]
    [--activity-no-user-action] [--activity-previous-is-top]
    [--activity-reorder-to-front] [--activity-reset-task-if-needed]
    [--activity-single-top] [--activity-clear-task]
    [--activity-task-on-home]
    [--receiver-registered-only] [--receiver-replace-pending]
    [--selector]
    [ | | ]


Sample applications:

Force to stop working for specified package,
adb shell "am -force-stop PACKAGE"
Ex:
     adb shell "am -force-stop com.android.browser"
In this case, if the browser is in the foreground, the browser would be stopped working and dismissed.
Note, the relative package would also be forced to stop working.

Trigger Android device to bring up the broswer and open a specified URL
adb shell "am start -a android.intent.action.VIEW -d url"
Within this command, start stands for doing something, -a stands for action, and -d stands for the data of action required.
Ex:
     adb shell "am start -a android.intent.action.VIEW -d http://google.com"
This example represents a view action for opening a url.

Make a phone call
adb shell "am start -a android.intent.action.CALL -d tel:phone_number"
Within this command, action is to make a call - android.intent.action.CALL, and action data is tel:phone_number.
Ex:
    am start -a android.intent.action.CALL -d tel:777
In this example, Android device would make a phone call with number 777.

Launch an activity with specified package name
adb shell "am start -n package/activity_name_or_full_activity_name_with_package"
Within this example, -n is followed by component name and the component would be launched by executing this command.
Ex:
    adb shell "am start -n com.android.settings/com.android.settings.Settings"
In this example, the settings activity would be brought up to top screen.

Create a new contact by am
adb shell "am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name 'Android Auto' -e phone 1234567890"
Within the command, action is insertion, -t stands for MIME type of inserting data in contact, -e stands for extra key that is defined in Manifest XML file, the name is followed by -e  for the extra key to be used for contact, and the value of extra key - name is followed by extra key - name. The parameters for -e phone 1234567890 has identical usage with -e name 'Anddroid Auto'. Contract app would fill the field of assigning extra keys for new contact in Android after executing above command. With respecting to action - android.intent.action.INSERT, it's necessary to have this configuration in Manifest XML. Or Android would do nothing for this command. That means in case you want to use this command on another activity, you need to check if this configuration's existed in Manifest XML file of the App you want to control.

There're a lot of parameters you can use for controlling Android. Above examples provided you some idea to extend your usage for your work

沒有留言:

張貼留言