Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does intent.resolveActivity(getPackageManager()) return null even though there is Activity to handle it?

I'm following the Android Codelabs, specifically I'm working on this Codelab with implicit intents.

The Codelab has the following method:

public void openWebsite(View view) {
   String url = mWebsiteEditText.getText().toString();

   Uri webpage = Uri.parse(url);
   Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

   if (intent.resolveActivity(getPackageManager()) != null) {
       startActivity(intent);
   } else {
       Log.d("ImplicitIntents", "Can't handle this intent!");
   }
}

The problem is that the intent.resolveActivity(getPackageManager()) returns null, but if I omit this and just call the startActivity(intent), it works fine and opens the Uri in Google Chrome.

I'm wondering why intent.resolveActivity(getPackageManager()) returns null, even thought the Uri can be opened in Google Chrome?

The Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.implicitintents">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

In this case the URL that we want to open comes from EditText field, so we can't use an intent-filter with android:host as described here.

like image 380
Αntonis Papadakis Avatar asked Sep 17 '25 05:09

Αntonis Papadakis


1 Answers

In case you haven't solve the problem yet. I don't know if you are using API level 30, but if you are you should check this new Package visibility restrictions. Package Visibility has changed and apps are no longer able to access the Package Manager directly. You need to add a element in your AndroidManifest file. In your case you would need something like this.

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>
like image 158
abrahamvee Avatar answered Sep 19 '25 19:09

abrahamvee