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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With