简体   繁体   English

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

[英]Why does intent.resolveActivity(getPackageManager()) return null even though there is Activity to handle it?

I'm following the Android Codelabs , specifically I'm working on this Codelab with implicit intents.我正在关注Android Codelabs ,特别是我正在以隐含的意图在这个Codelab 上工作。

The Codelab has the following method: 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!");
   }
}

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,但如果我省略它并只调用startActivity(intent) ,它可以正常工作并在 Google Chrome 中打开 Uri。

I'm wondering why intent.resolveActivity(getPackageManager()) returns null, even thought the Uri can be opened in Google Chrome?我想知道为什么intent.resolveActivity(getPackageManager())返回 null,即使认为 Uri 可以在 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 .在这种情况下,我们要打开的 URL 来自EditText字段,因此我们不能使用带有android:hostintent-filter ,如此所述。

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 .我不知道您是否使用的是 API 级别 30,但如果您是,您应该检查这个新的 Package 可见性限制 Package Visibility has changed and apps are no longer able to access the Package Manager directly. Package 可见性已更改,应用程序不再能够直接访问 Package 管理器。 You need to add a element in your AndroidManifest file.您需要在 AndroidManifest 文件中添加一个元素。 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>

I had the same problem.我有同样的问题。 I solved it by adding a request to use the browser in the manifest.我通过在清单中添加使用浏览器的请求来解决它。 Now my manifest file looks like this.现在我的清单文件看起来像这样。 Try reading this article .尝试阅读这篇文章

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.quakereport">
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".EarthquakeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

add permission添加权限

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

then add Queries然后添加查询

<queries>
   <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="https" />
   </intent>
</queries>

I hope it will solve your problem.我希望它能解决你的问题。 you can also check package visibility use cases您还可以查看package 可见性用例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "intent.resolveActivity(getPackageManager()) 到底在做什么?" - What exactly is intent.resolveActivity(getPackageManager()) doing? resolveActivity(getPackageManager()) != null 返回 null - resolveActivity(getPackageManager()) != null returns null intent.resolveActivity 在 API 30 中的 android.intent.action.OPEN_DOCUMENT 上返回 null - intent.resolveActivity returns null on android.intent.action.OPEN_DOCUMENT in API 30 在Activity.onCreate()中,为什么Intent.getExtras()有时会返回null? - In Activity.onCreate(), why does Intent.getExtras() sometimes return null? 为什么这个二进制搜索返回-1,即使该元素在数组中 - Why does this binary search return -1 even though the element is in the array JAXB为什么说“ xxx是一个接口,而JAXB无法处理接口”。 即使生成的类不是接口 - Why does JAXB say “xxx is an interface, and JAXB can't handle interfaces”. Even though the generated class is not an interface 即使我实现了它,活动也返回 null 并产生 NullPointerException - Activity returning null and producing NullPointerException even though I implemented it 为什么图片可以返回到上一个活动但不打算返回新活动 - why Image can return to previous activity but not intent to new activity 为什么我的方法说它必须返回一个类型字符串并给我一个错误,即使我的方法确实返回一个类型字符串 - Why does my method say it must return a type string and gives me an error even though my method does return a type string 为什么返回空值? - Why does this return null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM