Android中的Intent是什么? 有人能举个例子吗? intent的类型是什么,我们为什么要使用它们? 为什么intent在Android中如此重要?


当前回答

Intent是一个类,用来绑定执行某些操作的信息。

示例:当用户在活动中执行此类操作时,将数据从一个活动传递给另一个活动

目前的活动。

其他回答

意图是告诉Android你想做什么的一种方式。 换句话说,你要描述你的意图。意图可以用来向Android系统发出某个事件已经发生的信号。Android中的其他组件可以通过意图过滤器注册到这个事件。

以下是两种类型的意图

1.明确的意图

用于调用特定的组件。当您知道要启动哪个组件,但又不想让用户自由控制使用哪个组件时。例如,您有一个具有2个活动的应用程序。你想从活动A启动活动B。在这种情况下,你定义了一个明确的意图,目标是activityB,然后使用它直接调用它。

2.隐式意图

used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.

明确的意图总是传递给它的目标,无论它包含什么;没有参考过滤器。但是,只有当隐式意图能够通过组件的一个过滤器时,它才会被传递给组件

意图过滤器

如果一个intent被发送到Android系统,它将为这个intent确定合适的应用程序。如果已经为这种类型的intent注册了几个组件,Android会为用户提供打开其中一个的选择。

This determination is based on IntentFilters. An IntentFilters specifies the types of Intent that an activity, service, orBroadcast Receiver can respond to. An Intent Filter declares the capabilities of a component. It specifies what anactivity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. IntentFilters are typically defined via the AndroidManifest.xml file. For BroadcastReceiver it is also possible to define them in coding. An IntentFilters is defined by its category, action and data filters. It can also contain additional metadata.

如果一个组件没有定义Intent过滤器,它只能被显式Intent调用。

下面是定义过滤器的两种方法

1.清单文件

如果你在清单中定义了意图过滤器,你的应用程序不需要运行来响应在它的过滤器中定义的意图。Android在安装应用程序时注册过滤器。

2.广播接收器

如果您希望广播接收器仅在应用程序运行时接收意图。然后,您应该在运行时(以编程方式)定义您的意图过滤器。请记住,这只适用于广播接收器。

什么是意图?

Intent基本上是在组件(如活动、服务、广播接收器和内容提供者)之间传递的消息。因此,它几乎等同于传递给API调用的参数。API调用和通过意图调用组件之间的基本区别是:

API调用是同步的,而基于意图的调用是同步的 异步的。 API调用是编译时绑定,而基于意图的调用是 运行时绑定。

当然,通过使用所谓的显式意图(explicit intent),可以使intent完全像API调用一样工作,这将在后面解释。但通常情况下,隐式意图是可行的,这就是这里所解释的。

想要调用另一个组件的组件必须只表达其执行任务的意图。而任何其他存在的组件,并声称它可以通过意图过滤器完成这样的工作,由Android平台调用来完成这项工作。这意味着,两个组件都不知道彼此的存在,但仍然可以一起工作,为最终用户提供所需的结果。

这种组件之间的隐形连接是通过意图、意图过滤器和Android平台的组合来实现的。

这就带来了巨大的可能性,比如:

在运行时混合和匹配或者更确切地说即插即用组件。 用自定义开发的应用程序替换内置的Android应用程序 应用程序。 应用程序内部和应用程序之间的组件级重用。 如果我可以这么说的话,面向服务到最细粒度的级别。

以下是Android文档中关于intent的额外技术细节。

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a Background Service. An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are: action The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc. data The data to operate on, such as a person record in the contacts database, expressed as a Uri.

了解更多

Lars Vogel的教程 ProgrammerGuru文章 普遍的意图

意图是对要执行的操作的抽象描述。它可以与startActivity一起使用来启动一个Activity,与broadcastIntent一起使用来将它发送到任何感兴趣的BroadcastReceiver组件,与startService(Intent)或bindService(Intent, ServiceConnection, int)一起使用来与后台服务通信。

欲了解更多详情,请参阅以下链接:

1). http://developer.android.com/reference/android/content/Intent.html

2) http://developer.android.com/guide/topics/intents/intents-filters.html

3). http://www.vogella.de/articles/AndroidIntent/article.html

还有更多的文章可用。

意图用于从一个活动启动另一个活动。它主要用于几个目的,例如从一个活动向另一个活动发送数据,以及用于触发目的。

它们基本上有两种类型

隐式意图。 明确的意图。

作为一个初学者,我知道这些,我认为这将给一些关于机器人意图的基本概念

一个Android应用程序可以包含零个或多个活动。当应用程序有多个活动时,通常需要从一个活动导航到另一个活动。在Android中,你通过所谓的意图在活动之间导航。你可以通过使用putExtra()将一些数据传递给你想要通过intent启动的活动。