如果您的应用程序在 targetSdkVersion 30 上运行,那么 packageManager.queryIntentActivities(intent, 0) 将返回一个空列表。这是因为从 Android 11 (API 级别 30) 开始,queryIntentActivities() 方法会根据调用应用程序的声明进行过滤。为了解决这个问题,您需要在 AndroidManifest.xml 中使用 标签。

标签中,您可以声明您的应用程序可以启动的活动。这样,即使您的应用程序在 targetSdkVersion 30 上运行,queryIntentActivities() 也可以正常工作,并返回一个包含所有匹配活动的列表。

以下是使用 标签的示例:

<manifest ...>
  <application ...>
    <queries>
      <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" />
      </intent>
    </queries>
  </application>
</manifest>

在这个示例中,我们声明了应用程序可以启动所有具有 android.intent.action.VIEW 动作、android.intent.category.BROWSABLE 类别和 http 方案的活动。

通过在 AndroidManifest.xml 中使用 标签,您可以确保您的应用程序可以正确地使用 queryIntentActivities() 方法,并解决在 targetSdkVersion 30 以上时返回空列表的问题。


原文地址: https://www.cveoy.top/t/topic/p35N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录