intent.resolveActivity(getPackageManager())到底在做什么? [英] What exactly is intent.resolveActivity(getPackageManager()) doing?

查看:631
本文介绍了intent.resolveActivity(getPackageManager())到底在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历

I'm going through the the Android Developer Tutorials and I encountered a line of code that I do not understand.

这是代码行(位于Android Developer教程的第4页.)

This is the line of code (found on 4th page of the Android Developer tutorials.)

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

我有几个问题.

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

1) Intent.ACTION_VIEW documentation says that it simply displays data to the user. I understand that the app it chooses will depend on the type of data to be displayed. For instance, in this case, the webpage is being parsed as a uri data type. But what kind of implicit intent is being created here? In the backend, 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?

2)如果它找到多个可以处理此问题的类,会发生什么?它只是选择默认值还是要求用户选择要在其上运行的应用程序?当我在手机上运行代码时,默认情况下,它只是启动了Samsung Internet App.

2) What happens if it finds multiple classes that can handle this? Does it simply choose the default or ask the user to choose what app it wants to run in on? When I ran the code on my phone, it simply started the Samsung Internet App by default.

3)这实际上是我的主要问题.如果该意图已经链接到要启动的类,那么为什么还要麻烦 intent.resolveActivity(getPackageManager())?其文档指定如何如果返回一个类,它将处理该类.好吧,返回一个类.但是,返回的此类也没有在代码的任何行中并入"我的 intent 中,这使我认为 Intent.ACTION_VIEW 已经以某种方式对其进行了处理我.

3) This is actually my main question. If the intent has already been linked to a class to start, why bother with intent.resolveActivity(getPackageManager()) at all? Its documentation specifies how it handles the class if a class is returned. So alright, a class is returned. But also this class that it returns is not 'incorporated' in my intent at any line of my code, which makes me think that Intent.ACTION_VIEW has somehow already handled it for me.

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

This is going to a leap, 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?

4)此外,出于好奇,程序包管理器中的内容班?我在此处读到,它就像是应用程序数据的目录.我这样说对吗?它保留有关应用程序的什么样的数据,我该如何访问它们.该文档似乎并没有太大帮助.

4) Also, out of curiosity, what is inside the package manager class? I read here that it is like a directory of application data. Would I be correct in saying that? What kind of data does it keep about the application and how can I go about accessing them. The documentation doesn't seem to help much.

推荐答案

但是在这里创建什么样的隐式意图?

But what kind of implicit intent is being created here?

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

Um... an ACTION_VIEW Intent, to view the requested URL.

我是否可以认为Android遍历了手机中的所有类,看看哪个具有可以处理数据的意图过滤器并创建意图结构来启动它发现的此类?

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?

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

Activities are registered in the manifest. The OS basically has a database of all registered activities and their <intent-filter> details, and it uses that database to find candidates for any given implicit Intent.

它只是选择默认值还是要求用户选择要在其上运行的应用程序?

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

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

That depends on a variety of factors, including:

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

  • Whether the user chose a default handler for that sort of Intent (e.g., chose a default Web browser)

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

Whether you wrap the Intent using Intent.createChooser() to force a chooser

一个应用程序是否已经为URL注册了一个应用程序链接

Whether an app has registered an app link for the URL

如果该意图已经链接到要启动的类,那么为什么还要打扰intent.resolveActivity(getPackageManager())呢?

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

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

Because there may be zero activities to handle the Intent. Even for something as common as a Web browser, the specific user might not have access to a browser app (secondary user profiles, etc.). If you try starting an activity, and there is no match, you get an ActivityNotFoundException, so this check is trying to avoid such an exception.

但是我可以说Intent.ACTION_VIEW运行intent.resolveActivity(getPackageManager())或另一个功能相似并且以某种方式将返回的类合并到我的意图中的函数是正确的吗?

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 (如果有的话)的内容会更正确.

Not really. It would be more correct to say that resolveActivity() queries the database that I mentioned to see what would handle the Intent, if anything.

包管理器类中包含什么?

what is inside the package manager class?

一点点Java代码.它主要是通往核心OS进程的IPC网关,用于查询已安装应用程序及其功能等的数据库.

A little bit of Java code. It is mostly an IPC gateway to a core OS process, serving to query the database of installed apps, their capabilities, etc.

这篇关于intent.resolveActivity(getPackageManager())到底在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆