为什么 Intent.resolveActivity(getPackageManager()) 返回 null 即使有 Activity 来处理它? [英] Why does intent.resolveActivity(getPackageManager()) return null even though there is Activity to handle it?

查看:150
本文介绍了为什么 Intent.resolveActivity(getPackageManager()) 返回 null 即使有 Activity 来处理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Android Codelabs,特别是我正在研究this 带有隐式意图的 Codelab.

I'm following the Android Codelabs, specifically I'm working on this Codelab with implicit intents.

Codelab 有以下方法:

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!");
   }
}

问题是 intent.resolveActivity(getPackageManager()) 返回 null,但如果我省略它并只调用 startActivity(intent),它工作正常并在 Google Chrome 中打开 Uri.

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.

我想知道为什么 intent.resolveActivity(getPackageManager()) 返回 null,即使 Uri 可以在 Google Chrome 中打开?

I'm wondering why intent.resolveActivity(getPackageManager()) returns null, even thought the Uri can be opened in 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 字段,因此我们不能将 intent-filterandroid:host<一起使用/code> 如此处所述.

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.

推荐答案

如果您还没有解决问题.我不知道你是否使用 API 级别 30,但如果你是,你应该检查这个 新的包可见性限制.包可见性已更改,应用程序不再能够直接访问包管理器.您需要在 AndroidManifest 文件中添加一个元素.在您的情况下,您需要这样的东西.

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>

这篇关于为什么 Intent.resolveActivity(getPackageManager()) 返回 null 即使有 Activity 来处理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆