Android 究竟什么是意图过滤器?

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

What are Intent Filters exactly?

androidintentfilter

提问by Mina Samy

I've read lots of articles about intent filters and I really can't understand exactly what they do?

我已经阅读了很多关于意图过滤器的文章,但我真的无法完全理解它们的作用?

so please if anybody can explain to me with a clear example what is the role of intent filters exactly ?

所以请如果有人可以用一个明确的例子向我解释意图过滤器的作用究竟是什么?

thanks

谢谢

回答by Alex Martelli

I think it's well documented here. Summarizing, when (e.g.) you pass an intent to Context.startActivity, or Context.startService, or broadcast it with Context.sendBroadcast, etc, what activity or service (or, what set of broadcast receivers) is the intent delivered to?

我认为这里有很好的记录。总结一下,当(例如)您将意图传递给 Context.startActivity 或 Context.startService,或使用 Context.sendBroadcast 等广播它时,意图传递给哪些活动或服务(或哪组广播接收器)?

Answer: if the intent is "explicit", i.e., it has the component name field set, then it's delivered to the target component it designates -- this typically happens in communication within an application.

回答:如果意图是“明确的”,即它设置了组件名称字段,那么它会被传递给它指定的目标组件——这通常发生在应用程序内的通信中。

Otherwise ("implicit" intent, as is typical for communication betweenapplications), Android must determine the best activity or service (or set of receivers) on its own. How does Android do that? That's where filters come in, and I quote:

否则(“隐式”意图,这是应用程序之间通信的典型特征),Android 必须自行确定最佳活动或服务(或一组接收器)。安卓是怎么做到的?这就是过滤器的用武之地,我引用:

It does so by comparing the contents of the Intent object to intent filters, structures associated with components that can potentially receive intents. Filters advertise the capabilities of a component and delimit the intents it can handle. They open the component to the possibility of receiving implicit intents of the advertised type. If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.

它通过将 Intent 对象的内容与 Intent 过滤器、与可能接收 Intent 的组件关联的结构进行比较来实现。过滤器通告组件的功能并界定它可以处理的意图。它们打开组件接收广告类型的隐式意图的可能性。如果组件没有任何意图过滤器,则它只能接收显式意图。带有过滤器的组件可以接收显式和隐式意图。

The web page in question continues with many details and, at the end, a complete, simple example, and I think it would be redundant to copy and paste them here.

有问题的网页继续包含许多细节,最后是一个完整、简单的示例,我认为在此处复制和粘贴它们是多余的。

回答by Ben

Simply put, Intent filters are a way of telling the OS how to launch/communicate with the different activities/services/broadcast receivers in your app. So for example, if you want links that start with http://www.mycompany.comto lead people into your app, an intent filter is the way to accomplish that. Once its setup, anytime someone clicks on a link that starts with that (in any app) the user will be presented with the option to use your app to view that page. You've probably seen this with youtube urls. Likewise, if you want the 'share' link commonly seen in many apps to list your app, would use an intent filter to do that.

简而言之,意图过滤器是一种告诉操作系统如何启动/与应用程序中的不同活动/服务/广播接收器通信的方式。例如,如果您希望以http://www.mycompany.com开头的链接引导人们进入您的应用程序,那么意图过滤器就是实现这一目标的方法。设置完成后,只要有人点击以该开头的链接(在任何应用程序中),用户就会看到使用您的应用程序查看该页面的选项。您可能已经通过 youtube 网址看到了这一点。同样,如果您希望在许多应用程序中常见的“共享”链接列出您的应用程序,可以使用意图过滤器来执行此操作。

hope this helps...

希望这可以帮助...

回答by drawnonward

An intent filterlets the system know what data or service requests a component can handle. A component might be an activity, service or broadcast receiver.

一个意图过滤器让系统知道什么数据或组件可以处理服务请求。组件可能是活动、服务或广播接收器。

If you are writing an image viewer, you would add an intent filter (or several) to the manifest describing the images you can handle. If you are writing a file browser, you might package up the details of an image file in an intent, and the system would sift through intent filters until it found a best match to handle that image. The same goes for any type of data or service that might be passed from one component to the next.

如果您正在编写图像查看器,您可以向清单中添加一个(或多个)意图过滤器,描述您可以处理的图像。如果您正在编写文件浏览器,您可能会将图像文件的详细信息打包到一个意图中,系统将筛选意图过滤器,直到找到处理该图像的最佳匹配。这同样适用于可能从一个组件传递到下一个组件的任何类型的数据或服务。

回答by Jawad Zeb

Intent Filters is a way of telling OS that let me handle/provide these kind of Activities/Services

意图过滤器是一种告诉操作系统让我处理/提供这些类型的活动/服务的方式

By adding given filter to Manifest tells OS that i can also handle Sms service and whenever you send sms it will show in a list or you can also explicitly use this as your sms service.

通过将给定的过滤器添加到 Manifest,告诉操作系统我也可以处理短信服务,每当您发送短信时,它都会显示在列表中,或者您也可以明确地将其用作短信服务。

<intent-filter>

    <action android:name="android.intent.action.SENDTO" />

    <action android:name="com.example.code.SMS_INTENT" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:scheme="smsto" />

</intent-filter>

And to explicitly using this service call it like ...

并明确使用此服务调用它像......

 Intent intent = new Intent("com.example.code.SMS_INTENT", Uri.parse(uri));

 intent.putExtra("from", "code");

 startActivity(intent);

回答by Praveen Kumar Verma

intentFilters advertise the capabilities of a component and delimit the intents that can handle. an IntentFilter that a given Activity, Service or BroadcastReceiver will respond to a combination of action, data mime type, scheme, path, etc.

The intent by comparing the intent to intent filters based on three aspects:

1:- The intent action
2:- The intent data (both URI and data type)
3:- The intent category

action :
Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

data :
Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path, etc.) and MIME type.

category :
Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

intentFilters 通告组件的功能并界定可以处理的意图。一个 IntentFilter,给定的 Activity、Service 或 BroadcastReceiver 将响应动作、数据 MIME 类型、方案、路径等的组合。

通过比较 Intent 和基于三个方面的 Intent 过滤器来确定 Intent:

1:- 意图动作
2:- 意图数据(URI 和数据类型)
3:- 意图类别

action :
在 name 属性中声明接受的意图操作。该值必须是操作的文字字符串值,而不是类常量。

data :
声明接受的数据类型,使用一个或多个属性指定数据 URI(方案、主机、端口、路径等)和 MIME 类型的各个方面。

category :
在 name 属性中声明接受的意图类别。该值必须是操作的文字字符串值,而不是类常量。

For example, This activity handles "SEND" actions with text data.

例如,此活动使用文本数据处理“发送”操作。

<intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
</intent-filter>

回答by ognian

IntentFilters are used to declare a pattern of Intent attributes that the declaring component will respond to. You can specify with an IntentFilter that a given Activity, Service or BroadcastReceiver will respond to a combination of action, data mime type, scheme, path, etc. For example if you register an Activity with IntentFilter for ACTION_SEND with data type "text/plain", your Activity will be called each time the users wants to send some text.

IntentFilters 用于声明声明组件将响应的 Intent 属性模式。您可以使用 IntentFilter 指定给定的 Activity、Service 或 BroadcastReceiver 将响应动作、数据 MIME 类型、方案、路径等的组合。例如,如果您使用数据类型“text/plain”为 ACTION_SEND 注册一个 Activity ",每次用户想要发送一些文本时都会调用您的 Activity。

回答by BABU K

Intent filter tells the android system for which intent or event the android components(activity,service ,broadcast receiver) should listen.

意图过滤器告诉安卓系统安卓组件(活动、服务、广播接收器)应该监听哪个意图或事件。