我正在关注 Android Codelabs ,特别是我正在研究 this具有隐含 Intent 的 Codelab。
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 中打开?list 文件:
<?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
字段,所以我们不能使用 intent-filter
与 android:host
如所述here .
最佳答案
如果您还没有解决问题。
我不知道你是否使用 API 级别 30,但如果你是,你应该检查这个 new Package visibility restrictions .包可见性已更改,应用程序不再能够直接访问包管理器。您需要在 AndroidManifest 文件中添加一个元素。在你的情况下,你需要这样的东西。
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
关于java - 为什么 intent.resolveActivity(getPackageManager()) 会返回 null,即使有 Activity 来处理它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63358146/