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


当前回答

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

其他回答

意图是对要执行的操作的抽象描述。它可以与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

还有更多的文章可用。

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

它们基本上有两种类型

隐式意图。 明确的意图。

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

根据他们的文件:

Intent是一个在独立组件(比如两个活动)之间提供运行时绑定的对象。Intent表示应用程序的“做某事的意图”。你可以在各种各样的任务中使用intent,但最常见的是它们被用来启动另一个活动。

下面是示例链接: http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent

正如文档所描述的,为了启动一个活动(你还需要理解什么是活动),使用如下的意图

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(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.广播接收器

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

来自2013年Android构建者峰会上关于Android IPC/Binder框架的深入研究

在一些短小而有效的语句中可以理解其意图

Android supports a simple form of IPC(inter process communication) via intents Intent messaging is a framework for asynchronous communication among Android components (activity, service, content providers, broadcast receiver ) Those components may run in the same or across different apps (i.e. processes) Enables both point-to-point as well as publish subscribe messaging domains The intent itself represents a message containing the description of the operation to be performed as well as data to be passed to the recipient(s).

在这篇文章中,android架构师Dianne Hackborn给出了一个简单的答案,即它实际上是一个数据容器。

从android架构的角度来看:

Intent是一个用于进程间通信的数据容器。从android架构的角度来看,它是建立在Binder之上的。