Android 隐式意图 VS 显式意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2914881/
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
Android implicit intents VS explicit intents
提问by Alexander Oleynikov
Working with android I realized that implicit intents are good choice in most of cases due to their's flexibility. But what's about explicit intents? What are benefits of using them? What are common cases when it's a good practice to use them?
使用 android 我意识到隐式意图在大多数情况下是不错的选择,因为它们的灵活性。但是显式意图呢?使用它们有什么好处?什么是使用它们的好习惯的常见情况?
回答by Aditya Kamath
Implicit Intents do not directly specify the Android components which should be called, it only specifies action to be performed. A Uri can be used with the implicit intent to specify the data type.
隐式 Intent 不直接指定应该调用的 Android 组件,它只指定要执行的操作。Uri 可以与隐式意图一起使用来指定数据类型。
for example
例如
Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com"));
Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com"));
this will cause web browser to open a webpage. Android system searches for all components which are registered for the specific action and the data type.If many components are found then the user can select which component to use..
这将导致网络浏览器打开一个网页。Android 系统搜索为特定操作和数据类型注册的所有组件。如果找到许多组件,则用户可以选择使用哪个组件。
Explicit intents are used in the application itself wherein one activity can switch to other activity...Example Intent intent = new Intent(this,Target.class);
this causes switching of activity from current context to the target activity.
Explicit Intents can also be used to pass data to other activity using putExtra
method and retrieved by target activity by getIntent().getExtras()
methods.
在应用程序本身中使用显式意图,其中一个活动可以切换到其他活动......例如,Intent intent = new Intent(this,Target.class);
这会导致活动从当前上下文切换到目标活动。显式意图也可用于将数据传递给使用putExtra
方法的其他活动,并通过getIntent().getExtras()
方法由目标活动检索。
Hope this helped.
希望这有帮助。
回答by Cheryl Simon
You typically use explicit intents for starting activities within your own application. At that point you know exactly which activity you want to start, so there is no reason to go through the extra work of setting up the implicit intents.
您通常使用显式意图在您自己的应用程序中启动活动。此时,您确切地知道要启动哪个活动,因此没有理由进行设置隐式意图的额外工作。
回答by Varun Bhatia
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
Implicit Intents are 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.
An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters
显式意图用于调用特定组件。当您知道要启动哪个组件并且不想让用户自由控制要使用哪个组件时。例如,您有一个具有 2 个活动的应用程序。活动 A 和活动 B。您想从活动 A 启动活动 B。在这种情况下,您定义一个明确的意图定位活动 B,然后使用它直接调用它。
当您知道要做什么,但不知道应该启动哪个组件时,将使用隐式 Intent。或者,如果您想为用户提供在要使用的组件列表之间进行选择的选项。如果将这些 Intent 发送到 Android 系统,它会搜索为特定操作和数据类型注册的所有组件。如果只找到一个组件,Android 会直接启动该组件。例如,您有一个使用相机拍照的应用程序。您的应用程序的功能之一是您让用户可以发送他拍摄的照片。您不知道用户拥有可以发送照片的应用程序类型,并且您还希望为用户提供一个选项,让他在拥有多个外部应用程序时选择使用哪个外部应用程序。在这种情况下,您不会使用明确的意图。
一个明确的意图总是被传递给它的目标,不管它包含什么;不咨询过滤器。但是只有当它可以通过组件的过滤器之一时,才会将隐式意图传递给组件
回答by Daniel Raja Singh
1) Explicit Intent: component name developer know so, name specified in Intent.
1)Explicit Intent:组件名称开发者知道,名称在Intent中指定。
2) Implicit Intent: Not specified a component in Intent.
2) 隐式意图:未在意图中指定组件。
回答by Rohit Singh
KEY: When you know and when you don't know
KEY:当你知道和你不知道的时候
Explicit Intent:
显式意图:
Use explicit intent when you know exactly which Activity
can handle your request.
当您确切知道哪个Activity
可以处理您的请求时,请使用显式意图。
Example: You have a List Activity and when you click an item in the list it opens a Detail activity. In this case, you KNOWthat the details of the item can be shown or handled by DetailActivity.class
of your application.
So to perform this action you create an Intent by explicitly specifying the class name.
示例:您有一个列表活动,当您单击列表中的一个项目时,它会打开一个详细信息活动。在这种情况下,您知道项目的详细信息可以由DetailActivity.class
您的应用程序显示或处理。因此,要执行此操作,您可以通过显式指定类名来创建 Intent。
Intent showDeatil = new Intent(this,DetaiActivy.class);
startActivity(showDeatil);
Implicit Intent:
隐含意图:
Use implicit intent when you don't know which activity of which application/s can handle your request.
当您不知道哪个应用程序的哪个活动可以处理您的请求时,请使用隐式意图。
Example: You have a link. When you click the link it should open the webpage in some browser. You DON'T KNOWexactly which Activity in which application can handle your request. You just have a vague idea that its a webpage link so it should open a webpage in some browser when someone opens it. In this case, you just specify the ACTION and then OS takes care of the rest.
示例:您有一个链接。当您单击该链接时,它应该会在某个浏览器中打开该网页。您不知道哪个应用程序中的哪个 Activity 可以处理您的请求。您只是有一个模糊的想法,即它是一个网页链接,因此当有人打开它时,它应该在某个浏览器中打开一个网页。在这种情况下,您只需指定 ACTION,然后操作系统负责其余的工作。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
BONUS:
奖金:
How OS decides?
操作系统如何决定?
There is a term for it. It's called Intent Resolution.
In Intent resolution.
有一个术语。这叫做意图解析。
在意图解析中。
OS takes out the ACTION specified in your intent.
Goes in the PackageManager and looks up for all the registered activities with the matching ACTION all the application installed in your device.
Shows the list of all matching applications in a pop-up.
操作系统取出您的意图中指定的 ACTION。
进入 PackageManager 并查找所有已注册的活动,并使用匹配的 ACTION 安装在您的设备中的所有应用程序。
在弹出窗口中显示所有匹配应用程序的列表。
A safer way to write implicit intents.
一种更安全的方式来编写隐式意图。
Sometimes it's possible that there will be no Activity which matches with the ACTION. In this case, you will get a NullPointerException. So a more preferred way is this
有时可能没有与 ACTION 匹配的 Activity。在这种情况下,您将收到 NullPointerException。所以更优选的方式是这样
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
How to make your application get inside that popup list?
如何让您的应用程序进入该弹出列表?
Let's say you have written some Browser Application. If you want your applications to show up in the pop-up list when someone opens the link. Then you have to register your Activity with the action using Intent Filters AndroidManifest.xml file. Like this.
假设您编写了一些浏览器应用程序。如果您希望您的应用程序在有人打开链接时显示在弹出列表中。然后,您必须使用 Intent Filters AndroidManifest.xml 文件通过操作注册您的活动。像这样。
<application
..... >
......
<activity android:name=".YourBrowserActivity">
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="www.example.com" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
.....
</application>
References
Common Intent ACTIONS and their Intent-Filters list
More on Intent filters and Intent resolution
参考
Common Intent ACTIONS 及其 Intent-Filters 列表
更多关于 Intent 过滤器和 Intent 解析
回答by Ivan
From Docs:
从文档:
There are two types of intents:
有两种类型的意图:
- Explicit intentsspecify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, you can start a new activity in response to a user action or start a service to download a file in the background.
- Implicit intentsdo not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
- 显式意图指定组件以名称(完全限定的类名)开头。您通常会使用显式意图在您自己的应用程序中启动一个组件,因为您知道要启动的活动或服务的类名。例如,您可以启动新活动以响应用户操作或启动服务以在后台下载文件。
- 隐式意图不命名特定组件,而是声明要执行的一般操作,这允许来自另一个应用程序的组件处理它。例如,如果您想向用户显示地图上的位置,您可以使用隐式意图来请求另一个有能力的应用程序在地图上显示指定的位置。
回答by Ajay Vishwakarma
- Implicit intent - When we want to call the system components through intent to perform a particular task and we don't really know the name of the components to be used, the android system will show the desired list of applications to perform the task.
- Explicit intent - When we want to call the another activity with the full qualified name of the activity and of course we know the name of the activity.
- 隐式意图 - 当我们想通过意图调用系统组件来执行特定任务并且我们并不真正知道要使用的组件的名称时,android 系统将显示所需的应用程序列表来执行任务。
- 显式意图 - 当我们想用活动的全限定名称调用另一个活动时,当然我们知道活动的名称。
回答by Roshan S
Simply we can describe both intents like this..
我们可以简单地描述这两个意图。
Explicit Intents :They are used for communication inside a particular application.
显式意图:它们用于特定应用程序内部的通信。
eg : Consider an application which has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page which displays the username field which we entered before.In this case we use explicit intents because we need to change the activities and to carry data from one activity to the other activity(username field) in the same application.
例如:考虑一个应用程序,它有一个包含两个字段(比如用户名和密码)的登录页面。如果两者都为真,它将引导我们进入一个显示我们之前输入的用户名字段的页面。在这种情况下,我们使用显式意图,因为我们需要更改活动并将数据从一个活动传送到同一应用程序中的另一个活动(用户名字段)。
Implicit Intents :They are used for communication across two different applications.
隐式意图:它们用于跨两个不同应用程序的通信。
eg : consider a news app which describes about an accident in which the video of accident is recorded and uploaded in Facebook. While clicking on the link given in the news app it will direct us to Facebook .In this case the communication is between an activity in news app and and an activity in Facebook app.For this purpose we use Implicit Intents.
例如:考虑一个描述事故的新闻应用程序,其中事故视频被记录并上传到 Facebook。当点击新闻应用程序中给出的链接时,它会将我们定向到 Facebook。在这种情况下,通信是在新闻应用程序中的活动和 Facebook 应用程序中的活动之间进行的。为此,我们使用隐式意图。
I hope you can understand.
我希望你能够明白。
回答by KARSHIL SHETH
Implicit Intent
隐含意图
- It Pulls up the new Application without being specified which one to pull up.
- It specifies only action to be performed and do not directly specify Android Components.
- URI can be used with implicit Intent to specify the type of data.
- 它拉起新的应用程序而不指定拉起哪个应用程序。
- 它仅指定要执行的操作,不直接指定 Android 组件。
- URI 可以与隐式 Intent 一起使用来指定数据的类型。
Explicit Intent
显式意图
- It will pull up a specific application and is set while writing the code.
- It is used in application itself wherein one activity can switch to other activity.
- Used to pass data to other activity using put extra method and retrieved by target activity getIntent().
- It is always delivered to target even filter is not consulted.
- 它将拉出一个特定的应用程序并在编写代码时设置。
- 它用于应用程序本身,其中一个活动可以切换到其他活动。
- 用于使用 put extra 方法将数据传递给其他活动并由目标活动 getIntent() 检索。
- 即使不咨询过滤器,它也始终传递到目标。