📜  Android中的隐式意图和显式意图之间的区别

📅  最后修改于: 2021-05-10 13:55:23             🧑  作者: Mango

目的是一个消息传递对象,它告诉您要执行哪种操作。目的最重要的用途是活动的启动。目的是一种被动数据结构,其中包含要执行的动作的抽象描述。

目的主体:

意图中有两件重要的事情

  • action:要执行的常规操作,例如ACTION_VIEW,ACTION_EDIT,ACTION_MAIN等。
  • 数据:要处理的数据,例如联系人数据库中的人记录,表示为Uri

Android意向类型

android中有两种类型的Intent:

  1. 隐式和
  2. 明确的。

1.隐式意图

隐式意图未指定组件。在这种情况下,意图提供有关要调用的系统提供的可用组件的信息。例如,您可以编写以下代码来查看网页。

例子:

在下图中,未指定任何组件,而是执行了一个操作,即将打开一个网页。输入所需网页的名称,然后单击“单击”按钮。您的网页已打开。

内隐意图

2.明确意图

显式意图指定组件。在这种情况下,intent提供了要调用的外部类。

例子:

在下面的示例中,有两个活动(FirstActivity,SecondActivity)。当您在FirstActivity中单击“转到其他活动”按钮时,您将移至S econdActivity。当您单击SecondActivity中的“转到首页活动”按钮时,您将移至第一个活动。这是通过明确意图来完成的。

现在,让我们找出两者之间的主要区别。

差异表

Explicit Intent

Implicit Intent

As already mentioned above explicit intents are those in which the user has a clear vision and knows exactly which activity can handle the requests.

Example: When you have a Listview screen on tap of each item you will go to detail activity  

Intent = Intent(applicationContext,DetailActivity::class.java)

startActivity(intent) 

Implicit intents do not name a specific component like 

explicit intent, instead declare general action to perform,

which allows a component from another app to handle.

Example: When you tap the share button in any app you can see the Gmail, Bluetooth, and other sharing app options. Here user sends a request is the implicit intent request which can be handle by these Gmail, Bluetooth-like app.

Explicit intent can do the specific application action which is set by the code like changing activity, downloading the file in the background, etc.

It specifies the only action to be performed and 

does not directly specify Android Components.

In explicit intent, you can pass data to other activity by using the putExtra method and retrieve by using getIntent().

Example:

val intent = Intent(this, SecondActivity:: class.java).apply{

putExtra(“key”,”New Value”)

}

startActivity(intent)

Second Screen:

val secondIntent = intent.getStringExtra(“key”)

Here we just mention the action in the intent and OS 

decides which applications are suitable to handle the task, 

action across two different applications.

Explicit intents are used for communication inside the application. Like changing activities inside the application. They are used for communication across two different applications.
In explicit intent, the action target is delivered even the filter is not consulted. When you make an implicit call with the intent. OS look at the action and then it matches with all the filters intent-filters of all the registered activities of all application using PackageManager and then populates the result as a list, this process is called as intent Resolution. 
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!