简体   繁体   中英

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.

    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.

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?

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:

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

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

  • Whether an app has registered an app link for the URL

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

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.

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?

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?

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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