android中显式和隐式活动调用有什么区别?

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

What is the different between Explicit and implicit activity call in android?

androidandroid-activityandroid-implicit-intentexplicit-intent

提问by Adham

What is the difference between explicit and implicit activity call in android? If you explain the answer with a simple example will be good.

android中显式和隐式活动调用有什么区别?如果你用一个简单的例子来解释答案会很好。

回答by MichaelP

For example:

例如:

implicit activity call

隐式活动调用

In intent filter you create action for you activity, so other app can call your activity via this action as following:

在意图过滤器中,您为您的活动创建操作,因此其他应用程序可以通过此操作调用您的活动,如下所示:

<activity android:name=".BrowserActivitiy" android:label="@string/app_name">
   <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="http"/> 
   </intent-filter>
</activity>

And the other way to call implicit Intent is below:

另一种调用隐式 Intent 的方法如下:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);

Explicit activity call

显式活动调用

You make a call that indicate exactly which activity class:

您拨打电话以准确指示哪个活动类:

Intent intent = new Intent(this, ActivityABC.class);
intent.putExtra("Value", "This value for ActivityABC");
startActivity(intent);

Hope this help you understand more about Explicit and implicit activity call in android.

希望这能帮助您更多地了解 android 中的显式和隐式活动调用。

You can get more detail about Android Intent here

您可以在此处获取有关 Android Intent 的更多详细信息

回答by Varun Bhatia

  1. 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.
  2. 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.
  1. 显式意图用于调用特定组件。当您知道要启动哪个组件并且不想让用户自由控制要使用哪个组件时。例如,您有一个具有 2 个活动的应用程序。活动 A 和活动 B。您想从活动 A 启动活动 B。在这种情况下,您定义一个明确的意图定位活动 B,然后使用它直接调用它。
  2. 当您知道要做什么,但不知道应该启动哪个组件时,将使用隐式 Intent。或者,如果您想为用户提供在要使用的组件列表之间进行选择的选项。如果将这些 Intent 发送到 Android 系统,它会搜索为特定操作和数据类型注册的所有组件。如果只找到一个组件,Android 会直接启动该组件。例如,您有一个使用相机拍照的应用程序。您的应用程序的功能之一是您让用户可以发送他拍摄的照片。您不知道用户拥有可以发送照片的应用程序类型,并且您还希望为用户提供一个选项,让他在拥有多个外部应用程序时选择使用哪个外部应用程序。在这种情况下,您不会使用明确的意图。

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

一个明确的意图总是被传递给它的目标,不管它包含什么;不咨询过滤器。但是只有当它可以通过组件的过滤器之一时,才会将隐式意图传递给组件

回答by Simon

See Intent Resolution here

在此处查看意图解析

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

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

Explicit intents (activities) refer to a specific class, and in general, are only available to your packages. Implicit intents refer to intent filters where apps publicly announce that they can handle certain types of data or can provide specific services, e.g. send an email. With implicit intents, the users chooses which activity (typically a package) to use to handle the intent or if a default handler is set, it is launched.

显式意图(活动)指的是一个特定的类,通常只对你的包可用。隐式意图是指意图过滤器,应用程序公开宣布它们可以处理某些类型的数据或可以提供特定服务,例如发送电子邮件。对于隐式意图,用户选择使用哪个活动(通常是包)来处理意图,或者如果设置了默认处理程序,则启动它。

回答by Rohit Singh

When to use which?

什么时候用哪个?

Explicit intent:When you know which component can handle your request. So you explicitly mention that component namein the intent.

显式意图:当您知道哪个组件可以处理您的请求时。因此,您在意图中明确提及该组件名称

Intent i = new Intent(context,DetailActivity.class);  // DetailActivity.class is the component name
startActivity(i);

Implicit intent:When you don't know which application can handle your request then you mention the actionin intent and let the OS decide which application/s is/are suitable for your task.

隐式意图:当您不知道哪个应用程序可以处理您的请求时,可以在意图中提及该操作,并让操作系统决定哪些应用程序适合您的任务。

Example: Play music

示例:播放音乐

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(file);
startActivity(intent);

How OS decides?

操作系统如何决定?

When there is implicit call with an intent then OS takes out the action and then it matches with all the intent-filters of all the registered activities of all application using PackageManager and then populates the result as a list. It is called intent resolution
So there is a possibility that no application is available in your device which can handle your request. In that case, you will get NullPointer Exception.

当有意图的隐式调用时,操作系统取出动作,然后它使用 PackageManager 与所有应用程序的所有注册活动的所有意图过滤器匹配,然后将结果填充为列表。它被称为意图解析
因此有可能您的设备中没有可以处理您的请求的应用程序。在这种情况下,您将收到 NullPointer 异常。

So a safer way to call implicit intent would be this

所以一个更安全的调用隐式意图的方法是这样的

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(file);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

回答by SaadurRehman

Every time I get confused among these in either interview. So, I have summarised it like this, may it help someone to keep this difference in mind.

每次我在任何一次采访中都对这些感到困惑。所以,我是这样总结的,它可以帮助某人记住这种差异。

Summary:

概括:

In ImplicitIntents, the user implicitlytells the system WHATshould be done, without specifying who should do.

隐性意图,用户隐含地告诉系统什么应该做的,不指定谁应该做的。

In ExplicitIntents, the user explicitlytells the system WHOMto be triggered for whatever the work is.

ExplicitIntents 中,用户明确地告诉系统WHOM为任何工作而触发。

回答by YOmna

There are two types of intents:

有两种类型的意图:

  • Explicit Intent:

    While creating an Intent object when we explicitly specify and pass on the target component name directly in the intent, it's an explicit intent.

  • Implicit Intent:

    In this case we delegate the task of evaluating the registered components (registration is usually done using intent filters that we'll cover later) to Android based on the intent data and the intended action (like send an email, capture a photo, pin location on a map, etc.) that we pass. So Android will automatically fire up the component from the same app or some other app that can handle the intent message/job.The idea in this case is that, let's say we have to pin a location on a map, for that we don't have to code our own activity to handle that. Instead just pass on the location data to an app like Google maps that can do the job on our app's behalf.

  • 显式意图:

    我们在 Intent 中明确指定并直接传递目标组件名称时创建 Intent 对象时,它是一个显式意图。

  • 隐含意图:

    在这种情况下,我们根据意图数据和预期操作(例如发送电子邮件、拍摄照片、固定位置)将评估注册组件的任务(注册通常使用我们稍后介绍的意图过滤器完成)委托给 Android在地图上等)我们通过。因此,Android 将自动从同一个应用程序或其他一些可以处理意图消息/作业的应用程序中启动该组件。在这种情况下的想法是,假设我们必须在地图上固定一个位置,因为我们不必编写自己的活动来处理它。相反,只需将位置数据传递给可以代表我们的应用完成工作的 Google 地图等应用。

source : http://codetheory.in/android-intents/

来源:http: //codetheory.in/android-intents/

回答by Tisha

Implicit intent doesn't specify the component.Intent provides the information of a component

隐式意图不指定组件。Intent 提供组件的信息

Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent); 

whereas, Explicit intent specify the component.The intent provides information about the class.

显式意图指定组件。意图提供有关类的信息。

 Intent i = new Intent(this, ClassB.class);  
 startActivity(i);