2012年10月15日 星期一

Android Activity Manager 中文版

English Version


Android的系統主要有四個應用的元件。有以下這四種元件 - 活動(Activities)、服務(Services)、傳播接收器(Broadcast receivers)、與內容提供者(Content providers)。當中只有活動這個元件有使用者介面。

活動管理器 (Activity Manager)是一個可以控制這些元件的一個工具。

介紹
活動管理器必須使用Shell指令"am"。若要了解am有哪些功能與參數可以使用,可以使用指令 "adb shell am"。執行這個指令必須把你的Android裝置連接到電腦並且啟動USB偵錯模式。

以下是執行adb shell am的輸出結果:
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]
    [ | | ]


應用範例:

強制執行某個Package停止工作
adb shell "am -force-stop PACKAGE"
例:
     adb shell "am -force-stop com.android.browser"
這個指令會強制停止顯示在畫面上的瀏覽器跳出。使用這個指令來強制停止工作,相關的Package也會同步被強制停止。


瀏覽指定網頁
adb shell "am start -a android.intent.action.VIEW -d url"
這個指令的參數-a指的是要執行的動作(action),-d指的是執行動作所需要的資料(Data)。這邊的動作參數的值是
android.intent.action.VIEW,表示要做觀看的動作。-d參數後面的資料是網頁資料。
例:
     adb shell "am start -a android.intent.action.VIEW -d http://google.com"
這個指令會開啟預設的瀏覽器並且開啟google.com的網頁。

撥電話
adb shell "am start -a android.intent.action.CALL -d tel:phone_number"
這個指令使用了動作為android.intent.action.CALL,表示要執行呼叫的動作,而其後的資料值為tel:phone_number,其中tel:為撥電話的多媒體型態(MIME Type),tel:之後為所要撥出的電話號碼。
例:
    am start -a android.intent.action.CALL -d tel:777
這個範例會使手機撥打電話號碼777。

呼叫出一個指定的活動(Activity)
adb shell "am start -n package/activity_name_or_full_activity_name_with_package"
範例中,-n後面要指定package的名稱與斜線/及activity的名稱呼叫某個Activity被執行。

例:
    adb shell "am start -n com.android.settings/com.android.settings.Settings"
這個範例中,設定的使用者介面會被帶到前景。


利用am新增一個聯絡人
adb shell "am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name 'Android Auto' -e phone 1234567890"
這個範例的參數中,動作是
android.intent.action.INSERT,表示要做輸入的動作。-t後面接的值是多媒體的格式。這邊看到的多媒體格式為聯絡人的格式。-e後面接的是輸入欄位的名稱與欄位的值。這個範例輸入兩個欄位,一個是名稱(name),另一個是電話號碼(phone)。它們的值分別為android與1234567890。這些參數與動作必須在應用程式的Manifest XML中有出現,才能使用他。若使用在不同的應用程式,你可以先確認源碼是否有相關的參數與設定。

使用的方法還有很多種,這邊只舉幾個例子讓讀者們啟發,可以做延伸的應用。

沒有留言:

張貼留言