Android 为什么PackageManager不能从检测注册表(UI自动机)中找到活动?
为什么来自检测注册表UI automator的PackageManager找不到活动 意图{pkg=com.me.Activity1} 我的申请有3个入口点。三个活动,每个活动由一个图标表示 我正在尝试运行ui自动机测试,以按下图标并启动相应的活动 在Android Studio 2.2中,当我选择app并按Run时,它会正确运行并启动活动。但是,在ui自动机中,它在Nexus 5设备和模拟器上都失败 但是,按照文档中提供的示例,意图为空。 我发现了这一点,并试图修改。由于意图是显式创建的,因此它不再为null,但它不会启动活动-logcat显示它没有启动 调试显示resolveInfos.size为零。我猜上下文或检测注册表有问题Android 为什么PackageManager不能从检测注册表(UI自动机)中找到活动?,android,android-testing,android-studio-2.1,Android,Android Testing,Android Studio 2.1,为什么来自检测注册表UI automator的PackageManager找不到活动 意图{pkg=com.me.Activity1} 我的申请有3个入口点。三个活动,每个活动由一个图标表示 我正在尝试运行ui自动机测试,以按下图标并启动相应的活动 在Android Studio 2.2中,当我选择app并按Run时,它会正确运行并启动活动。但是,在ui自动机中,它在Nexus 5设备和模拟器上都失败 但是,按照文档中提供的示例,意图为空。 我发现了这一点,并试图修改。由于意图是显式创建的,因此
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
我也试过。。。getTargetContext而不是getContext,但失败
Context context = InstrumentationRegistry.getTargetContext();
谢谢你的帮助
@Test
public void userLaunchesActivity1() {
launchApp("com.me.Activity1");
}
void launchApp(String BASIC_SAMPLE_PACKAGE) {
Log.d("userLaunchesActivity1", "BASIC_SAMPLE_PACKAGE:"+BASIC_SAMPLE_PACKAGE);
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
// ======== example from android.com documentation - intent is null ====
// Description
// final Intent intent = context.getPackageManager()
// .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//context.startActivity(intent);
//
//=======stack overflow answer - does not launch activity====================
Intent intent = new Intent();
intent.setPackage(BASIC_SAMPLE_PACKAGE);
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
//======= this is not entered at all ===================
if(resolveInfos.size() > 0) {
ResolveInfo launchable = resolveInfos.get(0);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i = new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setComponent(name);
context.startActivity(i);
}
//===========================
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}