Android: Choose the application with which to open

2019-08-15 04:45发布

In Android applications, often the choice of selecting an app to open a link or do some other action is left to the user, i.e. the framework lets the user choose the app to do something.

For example, say you have a link to a tweet, and you are allowed to choose between:

  • Twitter
  • Chrome
  • Web Browser

What is the reason that the user is allowed to choose the application with which to open a link rather than with native application?

3条回答
虎瘦雄心在
2楼-- · 2019-08-15 05:04

Basically, you get a list of applications that handles the Intent that you send from your application.

For example, if you create this intent

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

Any application that can handle Intent.ACTION_DIAL will appear on your chooser list. In your case, when you handle links, Twitter, Chrome and Web Broswer are all applications that can handle these Intents, hence they appear in the chooser list.

You can read more about this here.

查看更多
成全新的幸福
3楼-- · 2019-08-15 05:07

According to 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.

and 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.

The idea is that in Android, a developer can let his app use another app on the device to perform some task, instead of having to recreate the same functionality within their own app. Often, there is more than one app that can perform the same task, and so Android allows the user to select which app they want to use for that task.

Basically, the framework attempts to provide the most general way of getting a task done: if the official Twitter app is not present, then you always have Chrome.

The way this is done in code is shown here.

查看更多
SAY GOODBYE
4楼-- · 2019-08-15 05:23

Google introduced implicit intents, but these intents are ambiguous, and Google decided to let the human solve the ambiguity.

In some cases the robot just cannot make the right choice. For example, how can robot decide what app must open a link with from the list: Opera, Chrome, Web browser?

It may look like apps like twitter and google translate are better suited for specific links, but in practice apps are glitchy (apps update automatically so that bugs may creep in, while the servers update implying that the apps update, so that the bugs may creep in even on devices with automatic updates turned off), and sometimes the user may wish to open a link with a browser rather than with the dedicated app.

查看更多
登录 后发表回答