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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:58:55  来源:igfitidea点击:

How to start activity in another application?

androidstart-activitycross-application

提问by user256239

I have application A defined as below:

我的应用程序 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>

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

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

回答by user256239

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.classin the code, so compile will fail.

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

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:

您还可以使用 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();

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

回答by azelez

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.

希望能帮助到你。