startActivity 不启动其他应用程序的新 Activity

startActivity does not launch a new Activity of other app

我有两个 android 应用程序(即 BIApp 和 EApp),我正在尝试从 BIApp 打开 EApp。 EApp有两个activity,一个是MainActivity,一个是LandingPageActivity。

当我尝试使用 startActivity(intent) 启动 MainActivity 时,它工作正常,但如果我尝试启动 LandingPageActivity。 startActivity(intent) 什么都不做。

如果 EApp 在后台打开,那么 startActivity(intent) 也适用于 LandingPageActivity。

下面是我正在使用的代码片段。

Intent intent = new Intent();
intent.setClassName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity");
startActivity(intent);

我想启动 LandingPageActivity。 我该怎么办?

试试这个:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);

并添加

android:exported="true"

并在 intent-filter 添加

<category android:name="android.intent.category.DEFAULT"/>

到清单中 LandingPageActivity 的声明。

我从来没有这样做过,但可能是其他应用程序无法访问 LandindPageActivity。在您的 EApp 清单中,您是否有像这样的 LandingPageActivity 意图过滤器?

<intent-filter>
    <action android:name="com.pkg.eapp.LandingPageActivity" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

试试这个:

Intent LaunchIntent =  getPackageManager().getLaunchIntentForPackage("com.pkg.eapp.LandingPageActivity");
startActivity(LaunchIntent);

希望这对您有所帮助。

试试这个,这对我有用

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);

阅读how to allow Other Apps to Start Your Activity


您只需将 android.intent.category.DEFAULT 类别添加到您的 Activity 到清单上。喜欢下面

<activity android:name="com.pkg.eapp.LandingPageActivity">
    <intent-filter>
        <action android:name="com.pkg.eapp.action.OPEN_LANDING_PAGE" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

并将其命名为

Intent intent = new Intent("com.pkg.eapp.action.OPEN_LANDING_PAGE");
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
// Or intent.setPackage("com.pkg.eapp");
startActivity(intent);