1.用adb shell去启动测试程序,如:
"]
S*E ac E
w
lJ8o/F0adb shell am start -n com.google.android.contacts/.ContactsActivity
am是一个脚本命令:51Testing软件测试网
C2I/T3a+tIs-n%G
0i6w'JDDXO
nq0# Script. to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am $*
V{&XI6KQ!M0不加任何参数运行am,我们可以看帮助信息:51Testing软件测试网l;C_1u'x&ZWZu
[root@localhost ~]# adb shell am
usage: am [start|broadcast|instrument|profile]
am start -D INTENT
am broadcast INTENT
am instrument [-r] [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
[-w] <:component>
am profile <PROCESS> [start <PROF_FILE>|stop]
INTENT is described with:
[-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <:component>] [-f <FLAGS>] [<URI>]
am 是在命令行启动android程序的一种方法,它是在cmds/am/src/com/android/commands/am/Am.java里实现的。51Testing软件测试网km1WHv}F
在函数runInstrument实现了instrument 命令,主要做了下面的事:
9\
xT#dco;x Se Sx5E01.参数解析51Testing软件测试网!e2X7J8{ ye
n]ES
2.创建一个InstrumentationWatcher对象,用来接收测试结果。51Testing软件测试网mI/^_*nw:^!Y7T5Lo
3.调用IActivityManager的startInstrumentation运行测试程序。51Testing软件测试网i(vE#p/tc
t
4.调用InstrumentationWatcher的waitForFinish函数等待测试结束。51Testing软件测试网8V N3f,U fb;g G6FN
51Testing软件测试网7a!Vt8]2Aa
private void runInstrument() {
...
try {
if (!mAm.startInstrumentation(cn, profileFile, 0, args, watcher)) {
System.out.println("INSTRUMENTATION_FAILED: " +
cn.flattenToString());
showUsage();
return;
}
} catch (RemoteException e) {
}
...
if (watcher != null) {
if (!watcher.waitForFinish()) {
System.out.println("INSTRUMENTATION_ABORTED: System has crashed.");
}
}
...
}
51Testing软件测试网 AV/G,q DZW"yh
startInstrumentation实际上是在services/java/com/android/server/am/ActivityManagerService.java里执行的,执行过程如下:51Testing软件测试网@.A(kb@h*un
1.调用PackageManager的getInstrumentationInfo获取InstrumentationInfo和ApplicationInfo。51Testing软件测试网D2z
G)?`/rx
2.uninstall原来的测试程序(如果前面运行了相同的测试程序,就会停止它)
g(V;j8U!@F+J"V03.运行新的测试程序。51Testing软件测试网L
Yc+f8N;Y+r$`
51Testing软件测试网ss];LX1\
ne
public boolean startInstrumentation(ComponentName className,
String profileFile, int flags, Bundle arguments,
IInstrumentationWatcher watcher) {
...
try {
ii = mContext.getPackageManager().getInstrumentationInfo(
className, 0);
ai = mContext.getPackageManager().getApplicationInfo(
ii.targetPackage, PackageManager.GET_SHARED_LIBRARY_FILES);
} catch (PackageManager.NameNotFoundException e) {
...
uninstallPackageLocked(ii.targetPackage, -1, true);
ProcessRecord app = addAppLocked(ai);
...
}