Android:调用另一个应用程序的活动 [英] Android : Call activity of another application

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

问题描述

我有两个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?

推荐答案

Grant,

这里的问题显然是对 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 中的每个动作都以一个 Intent 开始.对于活动尤其如此.每个 Activity 都有一个 Intent.为了使开发人员的界面更容易,您可以响应来自操作系统的 Intent,或者您可以从活动类创建一个 Intent 来使用.一般来说,最好的做法是做第一个选项.

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.

响应意图

在选择要响应的 Intent 时,您实际上可以响应任何 Intent.这称为操作.如果我创建了一个名为FOO"的 Intent,则 Bar Activity 可以拾取它并做出响应.然而,我们有约定,其中主要是在您创建的任何 Intent 之前添加您的包名称.例如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.

每个 Activity 可能响应不同的事件.这是在 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>

在上面,我们还将类别设置为 DEFAULT,这样除非用户更改它,否则我们将成为唯一响应我们自定义 Intent 的应用程序.然后我们调用 Intent 的方式是使用我们创建的 SAME NAME(即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".

为什么您的代码不起作用

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

上面的代码查找特定类型的 Intent 操作.记住当你制作 AndroidManifest 和你放置的第一个 Activity 时:

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() 只获取第一个 Activity 的 Intent.这就是我们制作自定义 Intent 的原因...首先,因为我们真的不希望它成为我们的第 3 个 Activity 作为我们的启动...其次,因为操作系统只会告诉我们启动 Activity.我们必须用我们自己的动作来告诉它(即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")

希望这会有所帮助,

模糊逻辑

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

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