android am 启动activity service or broadcast及参数传递

来源:互联网 发布:郑州网络电视台 编辑:程序博客网 时间:2024/05/21 13:06

http://blog.csdn.net/crazy_fire/article/details/7732802


android中启动activity、service或broadcast 的shell命令可通过adb shell am方式实现


关于am若查看其用法可在终端依次通过

sudo adb kill-server

sudo adb start-server

adb devices

adb shell

am


帮助信息结果如下:

[html] view plaincopy
  1. $ am  
  2. usage: am [subcommand] [options]  
  3.   
  4.     start an Activity: am start [-D] [-W] <INTENT>  
  5.         -D: enable debugging  
  6.         -W: wait for launch to complete  
  7.   
  8.     start a Service: am startservice <INTENT>  
  9.   
  10.     send a broadcast Intent: am broadcast <INTENT>  
  11.   
  12.     start an Instrumentation: am instrument [flags] <COMPONENT>  
  13.         -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)  
  14.         -e <NAME> <VALUE>: set argument <NAME> to <VALUE>  
  15.         -p <FILE>: write profiling data to <FILE>  
  16.         -w: wait for instrumentation to finish before returning  
  17.   
  18.     start profiling: am profile <PROCESS> start <FILE>  
  19.     stop profiling: am profile <PROCESS> stop  
  20.   
  21.     start monitoring: am monitor [--gdb <port>]  
  22.         --gdb: start gdbserv on the given port at crash/ANR  
  23.   
  24.     <INTENT> specifications include these flags:  
  25.         [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]  
  26.         [-c <CATEGORY> [-c <CATEGORY>] ...]  
  27.         [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]  
  28.         [--esn <EXTRA_KEY> ...]  
  29.         [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]  
  30.         [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]  
  31.         [-n <COMPONENT>] [-f <FLAGS>]  
  32.         [--grant-read-uri-permission] [--grant-write-uri-permission]  
  33.         [--debug-log-resolution]  
  34.         [--activity-brought-to-front] [--activity-clear-top]  
  35.         [--activity-clear-when-task-reset] [--activity-exclude-from-recents]  
  36.         [--activity-launched-from-history] [--activity-multiple-task]  
  37.         [--activity-no-animation] [--activity-no-history]  
  38.         [--activity-no-user-action] [--activity-previous-is-top]  
  39.         [--activity-reorder-to-front] [--activity-reset-task-if-needed]  
  40.         [--activity-single-top]  
  41.         [--receiver-registered-only] [--receiver-replace-pending]  
  42.         [<URI>]  


1. 启动activity  adb shell am start -n <package name>/<package name>.<activity>


设置参数

[java] view plaincopy
  1. COMMAND_START = "adb -s "+ deviceNum + " shell am start -n com.gps/com.compal.suspend.GPS -d "  
  2.                     + gpsOntime + "/" + gpsOfftime + "," + playCycle;  
  3.   
  4.   
  5. Runtime.getRuntime().exec(COMMAND_START)  


activity获取参数

intentData = getIntent().getDataString();



2. 关于service / broadcast的启动可参考一篇比较好的博文:

   http://blog.csdn.net/androidbluetooth/article/details/7664319


3. 当然am的作用并不只是如上这些,从am帮助信息即可明白


4.启动并传递参数的方法:

法一:查看am的帮助信息中的<INTENT>部分-d <DATA_URI>

      


法二:可参考另一篇好的博文:

http://www.yurushao.net/?p=784


比如,我们要启动的Acitvity所在的app是net.yurushao.demo,需要启动的是其中的ExampleActivity,并给他传递两个参数:
1. pid 整数,值为10
2. str 字符串,”hello, world”

那么,完整的命令为(在Android Shell中执行):

[java] view plaincopy
  1. am start -a android.intent.action.MAIN -n \  
  2. net.yurushao.demo/net.yurushao.demo.ExampleActivity \  
  3.  --ei pid 10 --es str "hello, world"  

简单说明一下,–ei表示参数类型为整型(extra integer),–es表示参数的类型为字符串(extra string),然后它们后面分别跟一个键值对,标识参数名和具体值。需要其他类型可以参考开头提到的那篇文章或者使用 am -h 查看帮助。

在ExampleActivity中获取传递来的参数也非常简单,在onCreate回调函数中添加:


[java] view plaincopy
  1. Intent intent = getIntent();  
  2. int pid = intent.getIntExtra("pid", -1); // 第二个参数为default value  
  3. String str = intent.getStringExtra("str");  

然后在AndroidManifest.xml中表示ExampleActivity的标签下,添加并接受 android.intent.action.MAIN

[html] view plaincopy
  1. <activity android:name="net.yurushao.demo.ExampleActivity" >  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.MAIN" />  
  4.     </intent-filter>  
  5. </activity>