java 意图类别的目的是什么?

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

What are the purpose of intent categories?

javaandroidandroid-intent

提问by LuckyLuke

Could someone please explain me the purpose of Intentcategories? When should I make my own and so on? The only thing thats written about Intentcategories in my book is that they can group intents?.

有人可以向我解释Intent类别的目的吗?我什么时候应该自己制作等等?Intent在我的书中关于类别的唯一内容是它们可以对意图进行分组?。

回答by Lukas Knuth

The category's alone are useless, they are used to describe a possible target for an "implicit intent"in an intent-filter.

该类别的单独是无用的,它们被用来描述一个可能的目标为“隐含意图”intent-filter

When you know which class/activity you want to launch and use startActivity()or startActivityForResult(), it's called an "explicit intent".

当您知道要启动和使用的类/活动startActivity()或 时startActivityForResult(),它被称为“显式意图”。

Here's an analogy for how implicit intents work:

这是隐式意图如何工作的类比:

Imagine all your applications sitting in a big room and doing nothing. Then, another application, let's say Dropbox, needs someone to open a PDF file. The Dropbox app goes to the system and says "Hey, somebody needs to open this PDF file..." (This is sending the implicit intent).

The system now goes to the room and yells "Which one of you can display a PDF file?". The applications that can stand up and the system sees them (these applications have an activity with a matching intent category).

It then offers you a dialog, in which you can choose one of the applications: Complete action using

想象一下,您的所有应用程序都坐在一个大房间里,什么也不做。然后,另一个应用程序,比如说 Dropbox,需要有人打开 PDF 文件。Dropbox 应用程序进入系统并说“嘿,有人需要打开这个 PDF 文件......”(这是发送隐式意图)。

系统现在进入房间并大喊“你们谁可以显示PDF文件?”。可以站起来并且系统看到它们的应用程序(这些应用程序具有具有匹配意图类别的活动)。

然后它会为您提供一个对话框,您可以在其中选择以下应用程序之一: 使用完成动作



If you want to make some of your Activity/BroadcastReceivers/Services available outside of your applications bounds, you can use the Android Manifest to declare an "intent filter" to it, so it gets opened when the system or an app launches an "implicit intent" that matches.

如果你想让你的一些 Activity/BroadcastReceivers/Services 在你的应用程序范围之外可用,你可以使用 Android Manifest 向它声明一个“意图过滤器”,这样当系统或应用程序启动“隐式过滤器”时它就会打开意图”匹配。

You do this (for example) for the Activity which should be opened from the launcher:

您为应该从启动器打开的活动执行此操作(例如):

<activity android:name=".SomeActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This listens to the ACTION_MAIN-action triggered by Androids Launcher (CATEGORY_LAUNCHER).

这会侦听ACTION_MAIN由 Androids Launcher ( CATEGORY_LAUNCHER)触发的-action 。

You have two child-elements in your "intent filter":

您的“意图过滤器”中有两个子元素:

  1. The action. This specifies what action the "intent filter" should listen to.
  2. One or multiple categorys. This specifies, how the activity should be called.
  1. action。这指定了“意图过滤器”应该听什么动作。
  2. 一个或多个categorys。这指定了应如何调用活动。

One of the categorys can be (for example) android.intent.category.DEFAULT, which tells the Activity to be launched normally in full-screen-mode. The android.intent.category.TAB-category for example declares this activity as a tab in a TabActivity, so it can only be opened as a Tab.

其中一个的categoryS可为(例如)android.intent.category.DEFAULT,它告诉要在全屏模式下正常启动的活动。该android.intent.category.TAB例如-category声明此活动作为一个标签TabActivity,所以它只能被打开的选项卡。

Another example would be adding the android.intent.category.PREFERENCE-category, which would declare the activity as your Settings-Activity.

另一个例子是添加android.intent.category.PREFERENCE-category,它将活动声明为您的设置活动。



Declaring your own categorys is neither possible nor necessary.

声明你自己的categorys 既不可能也没有必要。

Further more you'll need to understand that those events are triggered by the System/Another App and you can only specify if and how you want to react when they are triggered.

此外,您还需要了解这些事件是由系统/另一个应用程序触发的,并且您只能指定在触发它们时是否以及如何做出反应。