java程序包不可见

On Android 10 and earlier, apps could query the full list of installed apps on the system using methods like queryIntentActivities(). In most cases, this is far broader access than is necessary for an app to implement its functionality. With our ongoing focus on privacy, we’re introducing changes on how apps can query and interact with other installed apps on the same device on Android 11. In particular, we’re bringing better scoped access to the list of apps installed on a given device.

在Android 10及更低版本上,应用程序可以使用queryIntentActivities()类的方法查询系统上已安装应用程序的完整列表。 在大多数情况下,这比应用程序实现其功能所需的访问范围要广得多。 随着我们对隐私的不断关注,我们正在引入有关应用程序如何查询并与Android 11上同一设备上的其他已安装应用程序进行交互的更改。特别是,我们为给定位置上安装的应用程序列表带来了更好的范围访问设备。

To provide better accountability for access to installed apps on a device, apps targeting Android 11 (API level 30) will see a filtered list of installed apps by default. In order to access a broader list of installed apps, an app can specify information about apps they need to query and interact with directly. This can be done by adding a <queries> element in the Android manifest.

为了更好地负责访问设备上已安装的应用程序,默认情况下,以Android 11(API级别30)为目标的应用程序会看到已过滤的已安装应用程序列表。 为了访问更广泛的已安装应用程序列表,应用程序可以指定有关需要查询并直接与之交互的应用程序的信息。 这可以通过在Android清单中添加<queries>元素来完成。

For most common scenarios, including any implicit intents started with startActivity(), you won’t have to change anything! For other scenarios, like opening a specific third party application directly from your UI, developers will have to explicitly list the application package names or intent filter signatures like this:

对于大多数常见场景 ,包括以startActivity()开头的任何隐式意图,您都无需进行任何更改! 对于其他情况 ,例如直接从您的UI打开特定的第三方应用程序,开发人员将必须明确列出应用程序包名称或意图过滤器签名,如下所示:

<manifest package="com.example.game">
  <queries>
    <!-- Specific apps you interact with, eg: -->
    <package android:name="com.example.store" />
    <package android:name="com.example.service" />    <!--
         Specific intents you query for,
         eg: for a custom share UI
    -->
    <intent>
      <action android:name="android.intent.action.SEND" />
      <data android:mimeType="image/jpeg" />
    </intent>
  </queries>
  ...
</manifest>

If you use Custom Tabs to open URLs, you might be calling resolveActivity() and queryIntentActivities() in order to launch a non-browser app if one is available for the URL. In Android 11 there’s a better way to do this, which avoids the need to query other apps: the FLAG_ACTIVITY_REQUIRE_NON_BROWSER intent flag. When you call startActivity() with this flag, an ActivityNotFoundException will be thrown if a browser would have been launched. When this happens, you can open the URL in a Custom Tab instead.

如果使用“ 自定义选项卡”打开URL,则可能会调用resolveActivity()queryIntentActivities()来启动非浏览器应用程序(如果该URL可用)。 在Android 11中,有一种更好的方法可以避免查询其他应用程序: FLAG_ACTIVITY_REQUIRE_NON_BROWSER意向标志。 当使用此标志调用startActivity()时,如果将启动浏览器,则将抛出ActivityNotFoundException 。 发生这种情况时,您可以改为在“自定义”选项卡中打开URL。

try {
  val intent = Intent(ACTION_VIEW, Uri.parse(url)).apply {
    // The URL should either launch directly in a non-browser app
    // (if it’s the default), or in the disambiguation dialog    addCategory(CATEGORY_BROWSABLE)
    flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_REQUIRE_NON_BROWSER
  }  startActivity(intent)
} catch (e: ActivityNotFoundException) {
  // Only browser apps are available, or a browser is the default app for this intent
}

In rare cases, your app might need to query or interact with all installed apps on a device, independent of the components they contain. To allow your app to see all other installed apps, Android 11 introduces the QUERY_ALL_PACKAGES permission. In an upcoming Google Play policy update, look for guidelines for apps that need the QUERY_ALL_PACKAGES permission.

在极少数情况下,您的应用可能需要查询设备上所有已安装的应用或与之交互,而与它们包含的组件无关。 为了允许您的应用查看所有其他已安装的应用,Android 11引入了QUERY_ALL_PACKAGES权限。 在即将发布的Google Play政策更新中,查找需要QUERY_ALL_PACKAGES权限的应用的准则。

When targeting API level 30 and adding a <queries> element to your app, use the latest available release of the Android Gradle plugin. Soon we’ll be releasing updates to older Android Gradle plugin versions to add support for this element. You can find more information and use cases about Package Visibility in the developer documentation.

在将API级别定位为30并将一个<queries>元素添加到您的应用程序时,请使用最新可用的Android Gradle插件版本。 不久我们将发布对较旧的Android Gradle插件版本的更新,以添加对此元素的支持。 您可以在开发人员文档中找到有关程序包可见性的更多信息和用例。