安卓:另一个应用程序呼叫活动 [英] Android : Call activity of another application

查看:114
本文介绍了安卓:另一个应用程序呼叫活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Android应用程序,假设他们是A和B,A有五个活动,我想从B的按钮点击事件调用它的具体活动。 我测试调用一个应用程序从其他的这种方式:

I have two Android applications,suppose they are "A" and "B", "A" has five activities and I want to call its specific activity from button click event of "B". I tested this way of calling one application from another:

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");
startActivity(LaunchIntent);

com.testapp.ws是A软件包的名字。

"com.testapp.ws" is the package name of "A".

这从运行它的第一个活动A又不是从具体活动。 我怎么能叫A的指定活动?

This runs "A" from its first activity again not from the specific activity. How can I call A's specified activity?

推荐答案

格兰特,

这里的问题显然是Android应用程序模型的一种误解。 Commonsware是如何解决这个问题完全正确的。然而,如果没有理解Android基本原理的,我可以看到为什么你应用它的难度。因此,一个快速的解释:

The issue here is clearly a misunderstanding of the Android Application Model. Commonsware is absolutely correct about how to solve this problem. However, without understanding Android fundamentals, I can see why you are having difficulty applying it. So, a quick explanation:

在Android的每一个行动开始的意图。这是为活动尤其如此。每一项活动都有一个意向。为了使界面易于开发,可以从操作系统响应的意图,或者你可以创建一个从活动类的意图来使用。在一般情况下,它做的第一选择最佳实践。

Every action in Android begins with an Intent. This is particularly true for Activities. Every Activity has an Intent. To make the interface easy for the developers, you may respond to an Intent from the OS, OR you may create an Intent from the Activities class to use. In general, it is best practice to do the first option.

回应的意图

采摘的意图做出回应时,你可能会从字面上响应任何意图。这被称为一个动作。如果我创建了一个名为富的意图,该酒吧活动可以把它捡起来,并作出回应。我们有约定,但是,那些主要是prePEND你的包的名字给你做任何意向。例如com.company.package.FOO。简单地说,这是使我们避免冲突与其他应用程序。

When picking an Intent to respond to, you may literally respond to any Intent. This is called an Action. If I created an Intent called "FOO", the Bar Activity could pick it up and respond. We have conventions, however, and the primary of those is to prepend your package name to any Intent you make. For example "com.company.package.FOO". Simply put, this is so that we avoid collisions with other apps.

每一个活动可以应对不同的事件。这是在AndroidManifest.xml定义

Every Activity may respond to different events. This is defined in the AndroidManifest.xml.

<activity android:name="Activity3" ... >
    <intent-filter>
      <action android:name="com.company.package.FOO"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

上面,我们还可以设置类别默认的,所以,除非用户改变它,我们将响应我们的定制意向的唯一的应用程序。我们再调用意图的方式是通过使用我们创建的同名(即com.company.package.FOO)

Above, we also set the category to DEFAULT, so that unless the user changes it, we'll be the only app that responds to our custom Intent. The way that we then call the Intent is by using the SAME NAME that we created (i.e. "com.company.package.FOO")

startActivity(new Intent("com.company.package.FOO"));

这就是它是如何工作!你会简单地改变上述com.company.package.FOO你的包名(由应用程序定义)和有意义的事。一个例子是com.testapp.ws.SWAT_FLIES。

That's how it works! You would simply change the above "com.company.package.FOO" to your package name (defined by your application) and something meaningful. An example is "com.testapp.ws.SWAT_FLIES".

为什么你的code不起作用

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");

以上code相对于意向行动的一项具体形式。记住,当你做了AndroidManifest,你把第一个活动:

The above code looks for a specific KIND of Intent action. Remember when you made the AndroidManifest and the first Activity you put:

 <action android:name="android.intent.action.MAIN">
 <category android:name="android.intent.category.LAUNCHER">

嗯...... getLaunchIntentForPackage()只获取意图的第一个活动。这就是为什么我们做一个自定义的意图。首先,因为我们真的不希望它是我们的第三个活动是我们开始了......第二,监守操作系统将告诉我们,只有在启动活动。我们必须用我们自己的行动告诉它(即com.testapp.ws.SWAT_FLIES)

Well... getLaunchIntentForPackage() only gets the Intent for that first Activity. That's WHY we make a custom Intent... First, because we don't really want it to be our 3rd Activity to be our start up... And second, becuase the OS will tell us only the startup Activity. We have to tell it with our OWN action (i.e. "com.testapp.ws.SWAT_FLIES")

希望这有助于

FuzzicalLogic

FuzzicalLogic

这篇关于安卓:另一个应用程序呼叫活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆