简体   繁体   English

从 Android 应用程序 A,如何在应用程序 B 中打开非主要活动?

[英]From Android app A, how to open non main activity in app B?

I have app A and app B, both APK are generated with the same code base.我有应用程序 A 和应用程序 B,两个 APK 都是使用相同的代码库生成的。 App B is generated with a different flavor than app A, hence they have have different applicationId.应用 B 生成的风格与应用 A 不同,因此它们具有不同的 applicationId。

From app A, I'd like to start Activity1 in app B. Here is the code I'm using:从应用程序 A,我想在应用程序 B 中启动 Activity1。这是我正在使用的代码:

Intent intent=new Intent();
intent.setComponent(new ComponentName("com.packagename.appb", "com.packagename.appb.Activity1"));
startActivity(intent);

I'm getting the exception我得到了例外

ActivityNotFoundException: Unable to find explicit activity class {com.packagename.appb/com.packagename.appb.Activity1}; ActivityNotFoundException:无法找到显式活动类 {com.packagename.appb/com.packagename.appb.Activity1}; have you declared this activity in your AndroidManifest.xml?你有没有在你的 AndroidManifest.xml 中声明过这个活动?

But Activity1 is already declared in the manifest because it's an activity also available in app A但是 Activity1 已经在清单中声明,因为它是应用程序 A 中也可用的活动

What should I do ?我该怎么办 ?

You could use following approach if it fits.如果合适,您可以使用以下方法。 Create launcher intent:创建启动器意图:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.packagename.appb");

Put some data inside that Intent check on MainActivity onStart method, if data is there move to desired Activity and finally remove data from Intent .在 MainActivity onStart方法的Intent检查中放入一些数据,如果有数据,则移动到所需的Activity并最终从Intent删除数据。 For example:例如:

intent.putExtra("activityB", true);
startActivity(intent);

Inside B app MainActivity :在 B 应用程序MainActivity

@Override
protected void onStart() {
    super.onStart();
   Intent intent = getIntent();
   boolean shouldStartB = intent.getBooleanExtra("activityB", false);
    if(shouldStartB) {
      //start new Activity 
      intent.removeExtra("activityB"); //Don't forget to remove extra to prevent bug
   }
}

you should use the other app packageName/ class directly.您应该直接使用其他应用程序 packageName/ 类。 for example:例如:

Intent intent = new Intent("com.example.app");

You'd probably want to put a try/catch around for a ActivityNotFoundException for when the application is not installed您可能希望在未安装应用程序时为 ActivityNotFoundException 设置一个 try/catch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM