java - intent.resolveActivity(getPackageManager()) 究竟在做什么?

标签 java android android-intent

我正在浏览 the Android Developer Tutorials我遇到了一行我看不懂的代码。

这是代码行(在 Android 开发者教程的第 4 页上找到。)

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

我有几个问题。

1) Intent.ACTION_VIEW documentation说它只是向用户显示数据。我知道它选择的应用程序将取决于要显示的数据类型。例如,在这种情况下,网页被解析为 uri 数据类型。但是这里创建了什么样的隐含 Intent ?在后端,我是否可以将 Android 视为遍历我手机中的所有类并查看哪个具有可能处理数据的 Intent 过滤器并创建 Intent 结构以启动它找到的此类?

2) 如果它找到多个可以处理这个问题的类会怎样?它是简单地选择默认值还是要求用户选择它想要运行的应用程序?当我在手机上运行代码时,它只是默认启动了 Samsung Internet App。

3) 这实际上是我的主要问题。如果 Intent 已经链接到要启动的类,为什么还要使用 intent.resolveActivity(getPackageManager()) 呢?它的documentation指定返回类时如何处理该类。好吧,返回一个类。但是它返回的这个类也没有在我的代码的任何行的 intent 中“合并”,这让我认为 Intent.ACTION_VIEW 已经以某种方式处理了它我。

这将是一个飞跃,但我说 Intent.ACTION_VIEW 运行 intent.resolveActivity(getPackageManager()) 或其他函数是否正确是否类似并以某种方式将它返回的类合并到我的 Intent 中?

4) 另外,出于好奇,package manager class 里面是什么? ?我读了here它就像一个应用程序数据目录。我这样说对吗?它保留有关应用程序的何种数据以及我如何才能访问它们。该文档似乎没有太大帮助。

最佳答案

But what kind of implicit intent is being created here?

嗯...一个ACTION_VIEW Intent , 查看请求的 URL。

can I think of Android as going through all the classes in my phone and seeing which one has the intent filter that can possibly handle the data and creating the intent structure to start this class that it found?

Activity 在 list 中注册。操作系统基本上有一个数据库,包含所有已注册的 Activity 及其 <intent-filter>详细信息,并使用该数据库查找任何给定隐式 Intent 的候选对象.

Does it simply choose the default or ask the user to choose what app it wants to run in on?

这取决于多种因素,包括:

  • 用户是否为那种 Intent 选择了默认处理程序(例如,选择默认 Web 浏览器)

  • 是否包装Intent使用 Intent.createChooser()强制选择器

  • 应用是否已经为该URL注册了应用链接

If the intent has already been linked to a class to start, why bother with intent.resolveActivity(getPackageManager()) at all?

因为处理 Intent 的 Activity 可能为零.即使对于像 Web 浏览器这样常见的东西,特定用户也可能无法访问浏览器应用程序(二级用户配置文件等)。如果您尝试开始一项 Activity ,但没有匹配项,您会得到一个 ActivityNotFoundException ,因此此检查试图避免此类异常。

but would I be sort-of correct in saying that Intent.ACTION_VIEW runs intent.resolveActivity(getPackageManager()) or another function that does similar and somehow incorporates the class it returns into my intent?

不是真的。更正确的说法是 resolveActivity()查询我提到的数据库以查看处理 Intent 的内容,如果有的话。

what is inside the package manager class?

一些 Java 代码。它主要是核心操作系统进程的 IPC 网关,用于查询已安装应用程序的数据库、它们的功能等。

关于java - intent.resolveActivity(getPackageManager()) 究竟在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676934/

相关文章:

android - Match_parent 布局内部高度与 wrap_content 高度

android - SchemaFactory 不支持平台级别 8 中的 W3C XML Schema?

android - Android错误:权限拒绝:启动 Intent

android - android 4.0.3 上操作栏中的 "back button"似乎使我的应用程序崩溃

java - LogLevel.INFO 在 Jboss 日志中打印为 ERROR [STDERR],有什么问题吗?

java - 在 System.out.format 中使用 NumberFormat 导入

java - 查找 ArrayList<Object> 中的特定类型(即 Object = String 等)

java - 我正在 java 中旋转图像,但想保存旋转图像

android - 地理围栏形状,API v2 android

android - 电子邮件附件的 Intent 过滤器