请问有谁知道如何在代码中调用
am start -a ACTIVITY
命令。
或者是否可能直接从junit
代码中启动活动。
请问有谁知道如何在代码中调用
am start -a ACTIVITY
命令。
或者是否可能直接从junit
代码中启动活动。
以下是我用来从.jar文件启动活动的示例:
private boolean startSettings() {
try {
Runtime.getRuntime().exec(
"am start -n com.android.settings/.Settings");
sleep(1000);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
sleep(1000);
if (getUiDevice().getCurrentPackageName().contains(
"com.android.settings")) {
return true;
}
}
return false;
}
UiDevice device = UiDevice.getInstance(getInstrumentation());
final String TARGET_PACKAGE =
InstrumentationRegistry.getTargetContext().getPackageName();
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
device.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), 5000);
<!-- version-tag: VERSION_TAG -->
<!-- This line should already be at the end of build.xml -->
<import file="${sdk.dir}/tools/ant/uibuild.xml" />
<target name="start" description="Start App" depends="build, install">
<echo>Starting Navigation Example</echo>
<exec executable="${adb}" failonerror="true">
<arg value="shell" />
<arg value="am" />
<arg value="start" />
<arg value="-n" />
<arg value="com.example.android.navigationdrawerexample/.MainActivity" />
</exec>
</target>
<target name="mytest" description="Runs UI tests" depends="build, install, start">
<echo>Running UI Tests</echo>
<exec executable="${adb}" failonerror="true">
<arg value="shell" />
<arg value="uiautomator" />
<arg value="runtest" />
<arg value="${out.filename}" />
<arg value="-c" />
<arg value="com.example.android.navigationdrawerexample.MainTestCase" />
</exec>
</target>
com.app.myapp/.MainActivity
,但是不起作用。即使使用完全限定名称也不行。但是从命令提示符中运行adb shell am start -n com.app.myapp/.MainActivity
是有效的。有任何想法吗? - Mahendran