Android:调用另一个应用程序的活动

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

Android : Call activity of another application

androidandroid-activity

提问by Grant

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:

我有两个 Android 应用程序,假设它们是“A”和“B”,“A”有五个活动,我想从“B”的按钮点击事件中调用它的特定活动。我测试了这种从另一个应用程序调用一个应用程序的方式:

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

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

“com.testapp.ws”是“A”的包名。

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

这再次从其第一个活动而不是从特定活动中运行“A”。如何调用 A 的指定活动?

回答by Fuzzical Logic

Grant,

授予,

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 应用程序模型的误解。Commonsware 关于如何解决这个问题是绝对正确的。但是,在不了解 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.

Android 中的每个动作都以一个 Intent 开始。对于活动尤其如此。每个 Activity 都有一个 Intent。为了使开发人员易于使用该界面,您可以响应来自操作系统的 Intent,或者您可以从 Activities 类创建一个 Intent 以供使用。通常,最佳做法是执行第一个选项。

Responding to an Intent

响应意图

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.

在选择要响应的 Intent 时,您实际上可以响应任何 Intent。这称为操作。如果我创建了一个名为“FOO”的 Intent,则 Bar Activity 可以拾取它并做出响应。然而,我们有约定,其中主要是在您创建的任何 Intent 之前添加您的包名称。例如“com.company.package.FOO”。简而言之,这是为了避免与其他应用程序发生冲突。

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

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

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")

在上面,我们还将类别设置为 DEFAULT,这样除非用户更改它,否则我们将是唯一响应我们自定义 Intent 的应用程序。然后我们调用 Intent 的方式是使用我们创建的 SAME NAME(即“com.company.package.FOO”)

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

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".

这就是它的工作原理!您只需将上面的“com.company.package.FOO”更改为您的包名称(由您的应用程序定义)和一些有意义的内容。一个例子是“com.testapp.ws.SWAT_FLIES”。

Why your code doesn't work

为什么你的代码不起作用

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

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

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

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

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")

嗯... getLaunchIntentForPackage() 只获取第一个 Activity 的 Intent。这就是我们制作自定义 Intent 的原因...首先,因为我们真的不希望它成为我们的第 3 个 Activity 作为我们的启动...其次,因为操作系统只会告诉我们启动 Activity。我们必须用我们自己的动作来告诉它(即“com.testapp.ws.SWAT_FLIES”)

Hope this helps,

希望这可以帮助,

FuzzicalLogic

模糊逻辑

回答by CommonsWare

Step #1: Add an <intent-filter>to the third activity with a custom action:

第 1 步:<intent-filter>使用自定义操作向第三个活动添加一个:

<intent-filter>
  <action android:name="com.testapp.ws.SOMETHING_USEFUL"/>
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Step #2: Start that activity using an appropriate Intent:

第 2 步:使用适当的开始该活动Intent

startActivity(new Intent("com.testapp.ws.SOMETHING_USEFUL"));

回答by Abandoned Cart

There are cases where you may not be using two applications you specifically have editing capabilities for or you may not want to make custom intents, so in that case there is an alternative (with better error checking for availability):

在某些情况下,您可能没有使用两个专门具有编辑功能的应用程序,或者您可能不想制作自定义意图,因此在这种情况下,有一种替代方法(具有更好的可用性错误检查):

Intent intent = new Intent();
intent.setClassName("PACKAGE_NAME", "PACKAGE_NAME.TARGET_ACTIVITY");
if (isCallable(context, intent)) {
    // Attach any extras, start or start with callback
} else {
    // Respond to the application or activity not being available
}

Somewhere in the main class or in a subclass that handles general methods:

在主类或处理通用方法的子类中的某处:

public static boolean isCallable(Activity activity, Intent intent) {
    List<ResolveInfo> list = activity.getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

回答by bhavithra

here is the code to open an app(ex whatsapp) from another app

这是从另一个应用程序打开应用程序(ex whatsapp)的代码

public class MainActivity extends Activity{

公共类 MainActivity 扩展 Activity{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bClock = (Button) findViewById(R.id.button1);
    bClock.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager managerclock = getPackageManager();
    i = managerclock.getLaunchIntentForPackage("com.whatsapp");
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    }
    });

} }

} }