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.
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
[--R COUNT] [-S] [--opengl-trace]
am instrument [-r] [-e
am profile start
am profile stop [
am dumpheap [flags]
am set-debug-app [-w] [--persistent]
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
-R: repeat the activity launch
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
processes that are safe to kill -- that is, will not impact the user
am kill-all: Kill all background processes.
am broadcast: send a broadcast Intent.
am instrument: start an Instrumentation. Typically this target
is the form
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT). Use with
[-e perf true] to generate raw output for performance measurements.
common form is [-e
-w: wait for instrumentation to finish before returning. Required for
--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
-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.
Force to stop working for specified package,
adb shell "am -force-stop PACKAGE"
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.
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.
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.
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