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.