我正在跟随Android Codelabs,具体来说,我正在使用涉及隐式意图的此 Codelab。
该Codelab有以下方法:
问题在于
我想知道为什么
在这种情况下,我们想要打开的URL来自于
该Codelab有以下方法:
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!");
}
}
问题在于
intent.resolveActivity(getPackageManager())
返回了null,但如果我省略这个并直接调用startActivity(intent)
,它可以正常工作并在Google Chrome中打开Uri。我想知道为什么
intent.resolveActivity(getPackageManager())
返回null,即使Uri可以在Google Chrome中打开?
清单文件:<?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>
在这种情况下,我们想要打开的URL来自于
EditText
字段,所以我们不能像这里描述的那样使用带有android:host
的intent-filter
。