java - 为什么 intent.resolveActivity(getPackageManager()) 会返回 null,即使有 Activity 来处理它?

标签 java android android-intent

我正在关注 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-filterandroid: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/

相关文章:

java - 单选按钮选择决定下一页

android - 我如何检测字符串中的语言类型?

android - 如何在 Android 中查找 Intent 源?

android - 在 Android 主屏幕上创建类(不是 MainActivity)的快捷方式

java - LinearLayout换行而不是连接

java - 替换 fragment 时如何防止 "java.lang.IllegalStateException: Fragment already added"?

java - 如何以编程方式验证 log4j 2 是否已正确配置?

android - Firebase 云消息传递 : push notification and navigate to specific page of application

android - Android中如何判断两个context是否相同?

java - 在没有 Activity 的android中调用电话