Why doesn't PackageManager locate Activity fro

2019-06-24 18:36发布

Why doesn't PackageManager from Instrumentation Registry (UI-automator) locate the Activity?

Intent { pkg=com.me.Activity1 }

My application has 3 entry points. Three Activities, each represented by an icon.

I am trying to run a ui-automator test to press an icon and launch the corresponding activity.

In Android Studio 2.2, when I select app and press Run, then it runs correctly and launches the Activity. However, in ui-automator it fails both on the Nexus 5 device and in the emulator.

However, following the example provided in the documentation, "Accessing UI Components", the intent was null. I found this answer and tried to modify. Since the intent is explicitly created, it is no longer null, but it does not launch the Activity - the logcat shows that it did not launch.

Debugging shows that resolveInfos.size is zero. I am guessing there is something wrong with the context or the instrumentation registry.

    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

I also tried... getTargetContext() instead of getContext(), but it failed.

    Context context = InstrumentationRegistry.getTargetContext();

Any help appreciated.

@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);
}

0条回答
登录 后发表回答