Java – What do the different intent constructors do?

What do the different intent constructors do?… here is a solution to the problem.

What do the different intent constructors do?

Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.

Intent(Context packageContext, Class<?> cls)
Create an intent for a specific component.

Intent(String action, Uri uri, Context packageContext, Class<?> cls)
Create an intent for a specific component with a specified action and data.

While this may be obvious to some, can you help those of us who are difficult to “understand”?

For example, under what circumstances would I expect 1 to be better than another?

Solution

There are two types of intents:

  • Explicit intents Specifies the component (fully qualified class name) to start with a name. You typically use explicit intents to start components in your own application because you know the class name of the activity or service you want to start. For example, starting a new activity in response to a user action or starting a service to download a file in the background.
  • Implicit intents Instead of naming a specific component, declare a generic action to perform, which allows a component from another application to handle it. For example, if you want to show the user the location on the map, you can use the implicit intent to request another capable application to display the specified location on the map.

When you create an explicit intent that starts an activity or service, the system immediately starts the application component specified in the intent object.

When you create an implicit intent, the Android system starts by comparing the contents of the intent to the intent filter declared in the list file of other apps on the device. If an intent matches an intent filter, the component is started and an intent object is passed to it. If multiple intent filters are compatible, a dialog box appears for the user to select which app to use.

Read more: > Intents and Intent Filters


Back to your question, all three of these constructs are ways to create implicit/explicit intents. Where

  • Intent(String action, Uri uri) creates one implicit intent uses the given operation and URI.

  • Intent(Context packageContext, Class<?> cls) creates an explicit intent.

  • Intent(String action, Uri uri, Context packageContext, Class<?> cls) creates an explicit intent using the given action and URI.

Related Problems and Solutions