Android 从不同的包启动活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2741857/
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 07:11:09  来源:igfitidea点击:

launch activities from different package

android

提问by rayman

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.

我在包一中有活动 A,我想运行一个意图来启动包二中的活动 B。

How can I do this? Any samples will be welcome.

我怎样才能做到这一点?欢迎任何样品。

this is what ive done, and the error i get:

这就是我所做的,以及我得到的错误:

first activity ("MainActivity") in a package: com.abelski.currencyclient and second activity("SecondActivity" in a diffrent package: com.idan.second

包中的第一个活动(“MainActivity”):com.abelski.currencyclient 和第二个活动(不同包中的“SecondActivity”:com.idan.second

now i wanna call from MainActivity to SecondActivity.

现在我想从 MainActivity 调用到 SecondActivity。

ive added this line at the manifest of the MainActivity:

我在 MainActivity 的清单中添加了这一行:

 <activity android:name="com.idan.second.SecondApplicationActivity"></activity>

now in main Activity i got this button which run this line:

现在在主活动中,我得到了运行这一行的按钮:

Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class); 

and this is the rror:

这是错误:

04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):     

回答by

I am assuming that by "packages" you mean applications.

我假设“包”是指应用程序。

We have: - ApplicationA with FirstActivity - ApplicationB with SecondActivity

我们有: - ApplicationA 和 FirstActivity - ApplicationB 和 SecondActivity

If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:

如果在 ApplicationB 的 AndroidManifest.xml 文件中,在 SecondActivity 的声明中添加了一个意图过滤器,例如:

<activity android:name=".SecondActivity">
  <intent-filter>
    <action android:name="applicationB.intent.action.Launch" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

You can create an Intent to launch this SecondActivity from the FirstActivity with:

你可以创建一个 Intent 从 FirstActivity 启动这个 SecondActivity :

Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);

What this all means is:

这一切的意思是:

  • The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
  • When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
  • SecondActivity 对“applicationB.intent.action.Launch”的意图动作有一个过滤器
  • 当您使用该操作创建意图并调用“startActivity”时,系统将找到响应它的活动(如果有)

The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html

相关文档位于:https: //developer.android.com/reference/android/content/Intent.html

回答by Niver

I had the same problem and the solution was another level in the root of your package name.

我遇到了同样的问题,解决方案是包名根目录中的另一个级别。

If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated

如果您有两个包“com.first....”和“com.second...”,并且清单引用了“com.first”。然后您可以重构这两个包以重用第一部分。例如,“com.package.first”和“com.package.second”。然后你的清单也必须更新

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package">
...

    <activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

Your java code can create an intent and start the activity in the usual way:

您的 java 代码可以创建一个意图并以通常的方式启动活动:

Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);

回答by juejiang

If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party Appis simpler。With the first answer to that question, you don't need to make any modification to your second application.

如果你在这里提到的包与应用程序相同,我认为Android:为不同的第三方应用程序启动活动中的答案更简单。 有了该问题的第一个答案,您不需要对你的第二个应用程序。

回答by masbayu

First, you need to declare both packages and activities on Manifest :

首先,您需要在 Manifest 上声明包和活动:

        <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :

对于第二个活动,在 android:name --> .Packages name.activity 上,假设您的第二个软件包名称为 com.iden.second :

    <activity android:name=".second.SecondActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
        <intent-filter>
            <action android:name="android.intent.action.SECOND" />

            <category android:name="android.intent.category.SECOND" />
        </intent-filter>
    </activity>

then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :

然后在 MAINACTIVITY JAVA CLASS 上,假设您将使用按钮开始第二个活动:

public class MainActivity extends AppCompatActivity {

private Button mButtonSecond;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButtonSecond = findViewById(R.id.btn_second);

    mButtonSecond.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
            startActivity(newActivity);
        }
    });
   }
}

hope that can help, since i am not clear with the question structure

希望能有所帮助,因为我不清楚问题结构

回答by Samuh

Use explicit intents:

使用显式意图:

Intent intent = new Intent(context,ClassName.class);

where ClassName is from another package.

其中 ClassName 来自另一个包。

Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.

有时,您将不知道类的名称,在这种情况下,您将不得不依赖目标类通告的 Intent 来处理。