简体   繁体   English

"intent.resolveActivity(getPackageManager()) 到底在做什么?"

[英]What exactly is intent.resolveActivity(getPackageManager()) doing?

I'm going through the the Android Developer Tutorials<\/a> and I encountered a line of code that I do not understand.我正在阅读Android 开发人员教程<\/a>,遇到了一行我不理解的代码。

This is the line of code (found on 4th page of the Android Developer tutorials.)这是代码行(可在 Android 开发者教程的第 4 页找到。)

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

But what kind of implicit intent is being created here? 但是,这里正在创建什么样的隐式意图?

Um... an ACTION_VIEW Intent , to view the requested URL. 嗯... 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? 我是否可以将Android视为遍历手机中的所有类,看看哪个具有可以处理数据的意图过滤器并创建意图结构以启动它发现的此类?

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 . 操作系统基本上具有所有已注册活动及其<intent-filter>详细信息的数据库,并且使用该数据库查找任何给定隐式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: 这取决于多种因素,包括:

  • Whether the user chose a default handler for that sort of Intent (eg, chose a default Web browser) 用户是否为这种Intent选择了默认处理程序(例如,选择了默认的Web浏览器)

  • Whether you wrap the Intent using Intent.createChooser() to force a chooser 是否使用Intent.createChooser()包装Intent以强制选择器

  • Whether an app has registered an app link for the URL 应用程序是否已为URL注册了应用程序链接

If the intent has already been linked to a class to start, why bother with intent.resolveActivity(getPackageManager()) at all? 如果该意图已经链接到要启动的类,那么为什么还要打扰intent.resolveActivity(getPackageManager())呢?

Because there may be zero activities to handle the Intent . 因为可能有零个活动来处理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.). 即使对于像Web浏览器一样常见的东西,特定用户也可能无权访问浏览器应用程序(辅助用户配置文件等)。 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. 如果尝试启动活动,但没有匹配项,则会收到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? 但是我可以说Intent.ACTION_VIEW运行intent.resolveActivity(getPackageManager())或另一个类似但又以某种方式将返回的类合并到我的意图中的函数是否正确?

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. 如果说resolveActivity()查询我提到的数据库以查看将处理Intent (如果有的话),那将是更正确的说法。

what is inside the package manager class? 包管理器类里面有什么?

A little bit of Java code. 一点Java代码。 It is mostly an IPC gateway to a core OS process, serving to query the database of installed apps, their capabilities, etc. 它主要是通往核心OS进程的IPC网关,用于查询已安装应用程序及其功能等的数据库。

By the way this method can return null on Android 11 and higher caused by new restrictions addedd : https:\/\/developer.android.com\/about\/versions\/11\/privacy\/package-visibility<\/a> So to resolve this issue we must add a queries under manifest :顺便说一句,由于添加了新限制,此方法可以在 Android 11 及更高版本上返回 null: https<\/a> :\/\/developer.android.com\/about\/versions\/11\/privacy\/package-visibility 所以要解决这个问题,我们必须添加一个查询在清单下:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM