在软件包可见性受限的情况下实现常见用例

本文档介绍了应用与其他应用交互的几种常见用例。每个部分都提供了如何在软件包可见性有限的情况下实现应用功能的指导,如果您的应用以 Android 11(API 级别 30)或更高版本为目标平台,则需要考虑这一点。

当以 Android 11 或更高版本为目标平台的应用使用 Intent 启动另一个应用中的 Activity 时,最直接的方法是调用 Intent 并处理 ActivityNotFoundException 异常(如果没有可用的应用)。

如果您应用的一部分功能依赖于知道对 startActivity() 的调用是否能成功(例如显示 UI),请将元素添加到您应用清单的 <queries> 元素中。通常,这是一个 <intent> 元素。

打开网址

本部分介绍了在以 Android 11 或更高版本为目标平台的应用中打开网址的各种方式。

在浏览器或其他应用中打开网址

要打开网址,请使用包含 ACTION_VIEW Intent 操作的 Intent,如加载网页网址指南中所述。使用此 Intent 调用 startActivity() 后,会发生以下情况之一:

  • 网址在网页浏览器应用中打开。
  • 网址在支持将该网址作为深层链接的应用中打开。
  • 显示一个消歧对话框,让用户选择哪个应用打开网址。
  • 发生 ActivityNotFoundException,因为设备上没有可打开该网址的应用。(这不常见。)

    建议您的应用捕获并处理可能发生的 ActivityNotFoundException

由于 startActivity() 方法不需要软件包可见性即可启动另一个应用的 Activity,因此您无需将 <queries> 元素添加到您应用的清单中,也无需对现有 <queries> 元素进行任何更改。这对于打开网址的隐式和显式 Intent 都适用。

检查浏览器是否可用

在某些情况下,您的应用可能希望在尝试打开网址之前,验证设备上至少有一个可用的浏览器,或者某个特定的浏览器是默认浏览器。在这种情况下,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="https" />
</intent>

当您调用 queryIntentActivities() 并传递网页 Intent 作为参数时,返回的列表在某些情况下会包含可用的浏览器应用。如果用户已将网址配置为默认在非浏览器应用中打开,则该列表不包含浏览器应用。

在自定义标签页中打开网址

自定义标签页允许应用自定义浏览器的外观和体验。您无需添加或更改应用清单中的 <queries> 元素即可在自定义标签页中打开网址

但是,您可能希望检查设备是否具有支持自定义标签页的浏览器,或者使用 CustomTabsClient.getPackageName() 选择一个特定的浏览器通过自定义标签页启动。在这种情况下,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>

让非浏览器应用处理网址

即使您的应用可以使用自定义标签页打开网址,也建议您尽可能让非浏览器应用打开网址。要在您的应用中提供此功能,请尝试调用 startActivity(),并使用设置了 FLAG_ACTIVITY_REQUIRE_NON_BROWSER Intent 标志的 Intent。如果系统抛出 ActivityNotFoundException,则您的应用可以在自定义标签页中打开网址。

如果 Intent 包含此标志,当发生以下任一情况时,调用 startActivity() 会导致抛出 ActivityNotFoundException

  • 该调用会直接启动浏览器应用。
  • 该调用会向用户显示一个消歧对话框,其中唯一的选项是浏览器应用。

以下代码段展示了如何更新您的逻辑以使用 FLAG_ACTIVITY_REQUIRE_NON_BROWSER Intent 标志:

Kotlin

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.
    // So you can open the URL directly in your app, for example in a
    // Custom Tab.
    openInCustomTabs(url)
}

Java

try {
    Intent intent = new Intent(ACTION_VIEW, Uri.parse(url));
    // The URL should either launch directly in a non-browser app (if it's the
    // default) or in the disambiguation dialog.
    intent.addCategory(CATEGORY_BROWSABLE);
    intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REQUIRE_NON_BROWSER);
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Only browser apps are available, or a browser is the default.
    // So you can open the URL directly in your app, for example in a
    // Custom Tab.
    openInCustomTabs(url);
}

避免消歧对话框

如果您想避免显示用户在打开网址时可能看到的消歧对话框,而是希望在这些情况下自行处理网址,则可以使用设置了 FLAG_ACTIVITY_REQUIRE_DEFAULT Intent 标志的 Intent。

如果 Intent 包含此标志,当该调用会向用户显示消歧对话框时,调用 startActivity() 会导致抛出 ActivityNotFoundException

如果 Intent 既包含此标志又包含 FLAG_ACTIVITY_REQUIRE_NON_BROWSER Intent 标志,当发生以下任一情况时,调用 startActivity() 会导致抛出 ActivityNotFoundException

  • 该调用会直接启动浏览器应用。
  • 该调用会向用户显示消歧对话框。

以下代码段展示了如何同时使用 FLAG_ACTIVITY_REQUIRE_NON_BROWSERFLAG_ACTIVITY_REQUIRE_DEFAULT 标志:

Kotlin

val url = URL_TO_LOAD
try {
    // For this intent to be invoked, the system must directly launch a
    // non-browser app.
    val intent = Intent(ACTION_VIEW, Uri.parse(url)).apply {
        addCategory(CATEGORY_BROWSABLE)
        flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_REQUIRE_NON_BROWSER or
                FLAG_ACTIVITY_REQUIRE_DEFAULT
    }
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // This code executes in one of the following cases:
    // 1. Only browser apps can handle the intent.
    // 2. The user has set a browser app as the default app.
    // 3. The user hasn't set any app as the default for handling this URL.
    openInCustomTabs(url)
}

Java

String url = URL_TO_LOAD;
try {
    // For this intent to be invoked, the system must directly launch a
    // non-browser app.
    Intent intent = new Intent(ACTION_VIEW, Uri.parse(url));
    intent.addCategory(CATEGORY_BROWSABLE);
    intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REQUIRE_NON_BROWSER |
            FLAG_ACTIVITY_REQUIRE_DEFAULT);
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // This code executes in one of the following cases:
    // 1. Only browser apps can handle the intent.
    // 2. The user has set a browser app as the default app.
    // 3. The user hasn't set any app as the default for handling this URL.
    openInCustomTabs(url);
}

打开文件

如果您的应用处理文件或附件(例如检查设备是否可以打开给定文件),通常最简单的方法是尝试启动可以处理该文件的 Activity。为此,请使用包含 ACTION_VIEW Intent 操作和表示特定文件的 URI 的 Intent。如果设备上没有可用的应用,您的应用可以捕获 ActivityNotFoundException。在异常处理逻辑中,您可以显示错误或尝试自行处理文件。

如果您的应用必须提前知道另一个应用是否可以打开给定文件,请将以下代码段中的 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中。如果您在编译时已经知道文件类型,请包含该文件类型。

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.VIEW" />
  <!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
  <data android:mimeType="application/pdf" />
</intent>

然后,您可以通过使用 Intent 调用 resolveActivity() 来检查是否有可用的应用。

授予 URI 访问权限

注意:对于以 Android 11(API 级别 30)或更高版本为目标平台的应用,需要按照本节所述声明 URI 访问权限;对于所有应用(无论其目标 SDK 版本以及是否导出其内容提供程序),都建议这样做。

对于以 Android 11 或更高版本为目标平台的应用,若要访问内容 URI,您的应用 Intent 必须通过设置以下一个或两个 Intent 标志来声明URI 访问权限FLAG_GRANT_READ_URI_PERMISSIONFLAG_GRANT_WRITE_URI_PERMISSION

在 Android 11 及更高版本上,URI 访问权限为接收 Intent 的应用提供以下功能:

  • 根据给定的 URI 权限,读取或写入内容 URI 所表示的数据。
  • 获取包含与 URI 授权方匹配的内容提供程序的应用的可见性。包含内容提供程序的应用可能与发送 Intent 的应用不同。

以下代码段演示了如何添加 URI 权限 Intent 标志,以便以 Android 11 或更高版本为目标平台的另一个应用可以查看内容 URI 中的数据:

Kotlin

val shareIntent = Intent(Intent.ACTION_VIEW).apply {
    flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
    data = CONTENT_URI_TO_SHARE_WITH_OTHER_APP
}

Java

Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setData(CONTENT_URI_TO_SHARE_WITH_OTHER_APP);

连接到服务

如果您的应用需要与并非自动可见的服务进行交互,您可以在 <queries> 元素中声明相应的 Intent 操作。以下部分提供了使用常用服务的示例。

连接到文本转语音引擎

如果您的应用与文本转语音 (TTS) 引擎交互,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.TTS_SERVICE" />
</intent>

连接到语音识别服务

如果您的应用与语音识别服务交互,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.speech.RecognitionService" />
</intent>

连接到媒体浏览器服务

如果您的应用是客户端媒体浏览器应用,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.media.browse.MediaBrowserService" />
</intent>

提供自定义功能

如果您的应用需要根据与其他应用的交互执行可自定义的操作或显示可自定义的信息,则可以使用Intent 过滤器签名,将其作为清单中 <queries> 元素的一部分来表示该自定义行为。以下部分提供了几种常见场景的详细指南。

查询短信应用

如果您的应用需要有关设备上安装的短信应用集的信息(例如检查哪个应用是设备的默认短信处理程序),请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.SENDTO"/>
  <data android:scheme="smsto" android:host="*" />
</intent>

创建自定义分享表

尽可能使用系统提供的分享表。另外,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.SEND" />
  <!-- Replace with the MIME type that your app works with, if needed. -->
  <data android:mimeType="image/jpeg" />
</intent>

否则,与 Android 11 之前的 Android 版本相比,在您的应用逻辑中构建分享表(例如调用 queryIntentActivities())的过程保持不变。

显示自定义文本选择操作

当用户在您的应用中选择文本时,文本选择工具栏会显示可对所选文本执行的可能操作集。如果此工具栏显示来自其他应用的自定义操作,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.intent.action.PROCESS_TEXT" />
  <data android:mimeType="text/plain" />
</intent>

显示联系人的自定义数据行

应用可以向联系人提供程序添加自定义数据行。联系人应用要显示此自定义数据,需要能够执行以下操作:

  1. 从其他应用读取 contacts.xml 文件。
  2. 加载与自定义 MIME 类型对应的图标。

如果您的应用是联系人应用,请将以下 <intent> 元素作为 <queries> 元素的一部分包含在您的清单中:

<!-- Place inside the <queries> element. -->
<!-- Lets the app read the contacts.xml file from other apps. -->
<intent>
  <action android:name="android.accounts.AccountAuthenticator" />
</intent>
<!-- Lets the app load an icon corresponding to the custom MIME type. -->
<intent>
  <action android:name="android.intent.action.VIEW" />
  <data android:scheme="content" android:host="com.android.contacts"
        android:mimeType="vnd.android.cursor.item/*" />
</intent>