Android:选择用于打开 link 的应用程序

Android: Choose the application with which to open a link

在 Android 应用程序中,通常选择应用程序打开 link 或执行其他操作的选择留给用户,即框架 lets 用户 选择 应用来做某事。

例如,假设您有一个 link 推文,您可以选择:

允许用户选择打开 link 而不是本机应用程序的应用程序的原因是什么?

基本上,您会得到一个应用程序列表,这些应用程序处理您从应用程序发送的 Intent

例如,如果您创建此意图

Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);

任何可以处理 Intent.ACTION_DIAL 的应用程序都会出现在您的选择列表中。在您的情况下,当您处理链接时,Twitter、Chrome 和 Web Broswer 都是可以处理这些 Intent 的应用程序,因此它们出现在选择器列表中。

您可以阅读更多相关信息 here

根据this

An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can and you'd like the user to pick which app to use.

this:

The real power of intents lies in the concept of implicit intents. An implicit intent simply describes the type of action to perform (and, optionally, the data upon which you’d like to perform the action) and allows the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.

想法是,在 Android 中,开发人员可以让他的应用程序使用设备上的另一个应用程序来执行某些任务,而不必在他们自己的应用程序中重新创建相同的功能。通常,有不止一个应用程序可以执行相同的任务,因此 Android 允许用户 select 他们 他们 想要使用哪个应用程序来完成该任务.

基本上,该框架试图提供最通用的方式来完成任务:如果官方 Twitter 应用程序不存在,那么您总是 Chrome.

在代码中完成此操作的方式如 here.

所示

Google 引入了隐式意图,但这些意图是模棱两可的,Google 决定让人类解决模棱两可的问题。

在某些情况下,机器人无法做出正确的选择。例如,机器人如何决定必须使用以下列表打开 link 的应用程序:Opera、Chrome、Web 浏览器?

Twitter 和 google 翻译之类的应用程序看起来更适合特定的 links,但实际上应用程序存在故障(应用程序会自动更新,因此可能会出现错误,而服务器更新意味着应用程序更新,因此即使在关闭自动更新的设备上,错误也可能会蔓延),有时用户可能希望使用浏览器而不是专用应用程序打开 link。