Android中使用am命令实现在命令行启动程序详解

在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am.

复制代码 代码如下:

usage: am [subcommand] [options]

    start an Activity: am start [-D]
        -D: enable debugging

    send a broadcast Intent: am broadcast

    start an Instrumentation: am instrument [flags]
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e : set argument to
        -p : write profiling data to
        -w: wait for instrumentation to finish before returning

    start profiling: am profile start
    stop profiling: am profile stop

    specifications include these flags:
        [-a ] [-d ] [-t ]
        [-c [-c ] ...]
        [-e|--es ...]
        [--ez ...]
        [-e|--ei ...]
        [-n ] [-f ] []


启动的方法为
复制代码 代码如下:

# am start -n 包(package)名/活动(activity)全路径

查看包中每个activity的全路径的方法:
1、安装并启动“固件工具箱”
2、进入“App manager”,显示系统中所有安装程序,长按想通过命令行启动的程序。
3、弹出的窗口中选择"Advanced Freeze",该程序所有的activity就会全部显示出来
activity有两种显示格式,一种直接显示全路径,另一种在括号里显示相对路径,相对路径加上包名即是全路径。

几个启动指定程序activity的例子
Music 和 Video(音乐和视频)的启动方法为:

复制代码 代码如下:

# am start -n com.android.music/com.android.music.MusicBrowserActivity
# am start -n com.android.music/com.android.music.VideoBrowserActivity
# am start -n com.android.music/com.android.music.MediaPlaybackActivity

Camera(照相机)的启动方法为:
复制代码 代码如下:

# am start -n com.android.camera/com.android.camera.Camera

Browser(浏览器)的启动方法为:
复制代码 代码如下:

# am start -n com.android.browser/com.android.browser.BrowserActivity

启动浏览器 :
复制代码 代码如下:

am start -a android.intent.action.VIEW -d  http://www.google.cn/

拨打电话 :
复制代码 代码如下:

am start -a android.intent.action.CALL -d tel:10086

启动 google map 直接定位到北京 :
复制代码 代码如下:

am start -a android.intent.action.VIEW geo:0,0?q=beijing