如何在另一个应用程序中启动活动?

发布于 2024-08-20 14:18:48 字数 542 浏览 4 评论 0原文

我的应用程序 A 定义如下:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.example.MyExampleActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

现在在应用程序 B 中,如何编写代码来启动应用程序 A 中的活动?谢谢!

I have application A defined as below:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.example.MyExampleActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Now in application B, how can I write the code to start the activity in application A? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染墨丶若流云 2024-08-27 14:18:48

如果你们遇到“Permission Denial:starting Intent ...”错误,或者应用程序在启动应用程序期间无故崩溃 - 那么在清单中使用此单行代码

android:exported="true"

请小心完成(); ,如果你错过了,应用程序就会被冻结。如果提到该应用程序将是一个流畅的启动器。

finish();

另一个解决方案仅适用于同一应用程序中的两个活动。就我而言,应用程序 B 不知道代码中的类 com.example.MyExampleActivity.class,因此编译将失败。

我在网上搜索了一下,发现了下面这样的东西,效果很好。

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);

您还可以使用 setClassName 方法:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.hotfoot.rapid.adani.wheeler.android", "com.hotfoot.rapid.adani.wheeler.android.view.activities.MainActivity");
startActivity(intent);
finish();

您还可以将值从一个应用程序传递到另一个应用程序:

Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hotfoot.rapid.adani.wheeler.android.LoginActivity");
if (launchIntent != null) {
    launchIntent.putExtra("AppID", "MY-CHILD-APP1");
    launchIntent.putExtra("UserID", "MY-APP");
    launchIntent.putExtra("Password", "MY-PASSWORD");
    startActivity(launchIntent);
    finish();
} else {
    Toast.makeText(getApplicationContext(), " launch Intent not available", Toast.LENGTH_SHORT).show();
}

If you guys are facing "Permission Denial: starting Intent..." error or if the app is getting crash without any reason during launching the app - Then use this single line code in Manifest

android:exported="true"

Please be careful with finish(); , if you missed out it the app getting frozen. if its mentioned the app would be a smooth launcher.

finish();

The other solution only works for two activities that are in the same application. In my case, application B doesn't know class com.example.MyExampleActivity.class in the code, so compile will fail.

I searched on the web and found something like this below, and it works well.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);

You can also use the setClassName method:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.hotfoot.rapid.adani.wheeler.android", "com.hotfoot.rapid.adani.wheeler.android.view.activities.MainActivity");
startActivity(intent);
finish();

You can also pass the values from one app to another app :

Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hotfoot.rapid.adani.wheeler.android.LoginActivity");
if (launchIntent != null) {
    launchIntent.putExtra("AppID", "MY-CHILD-APP1");
    launchIntent.putExtra("UserID", "MY-APP");
    launchIntent.putExtra("Password", "MY-PASSWORD");
    startActivity(launchIntent);
    finish();
} else {
    Toast.makeText(getApplicationContext(), " launch Intent not available", Toast.LENGTH_SHORT).show();
}
メ斷腸人バ 2024-08-27 14:18:48

如果两个应用程序具有相同的签名(意味着两个应用程序都是您的并且使用相同的密钥签名),您可以按如下方式调用其他应用程序活动:

Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(CALC_PACKAGE_NAME);
startActivity(LaunchIntent);

希望它有帮助。

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows:

Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(CALC_PACKAGE_NAME);
startActivity(LaunchIntent);

Hope it helps.

瑾夏年华 2024-08-27 14:18:48

确保您尝试启动的目标活动可导出。要检查这一点,请转到目标应用程序的 AndroidManifest.xml 文件,并检查活动的声明是否包含以下代码:

android:exported="true"

这已得到确认。然后,您可以在要启动目标活动的源应用程序中使用此代码。

try {
      Intent().apply {
      component = ComponentName(
                "com.example.application",
                "com.example.application.ActivityName"
               )
      startActivity(this)
      }
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace(System.out)
    }

Make sure the target activity you are trying to start is exportable. To check this, go to the AndroidManifest.xml file of the target application and check the activity's declaration has the below code:

android:exported="true"

One this is confirmed. Then you can use this code in the source application from where you want to start the target activity.

try {
      Intent().apply {
      component = ComponentName(
                "com.example.application",
                "com.example.application.ActivityName"
               )
      startActivity(this)
      }
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace(System.out)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文