Intent resolution

When the system receives an implicit intent to start an activity, it searches for the best activity for the intent by comparing the it to intent filters based on three aspects:

  • Action.
  • Data (both URI and data type).
  • Category.

Action test

To specify accepted intent actions, an intent filter can declare zero or more  elements, as shown in the following example:


    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.VIEW" />
    ...

To pass this filter, the action specified in the Intent must match one of the actions listed in the filter.

Category test

To specify accepted intent categories, an intent filter can declare zero or more  elements, as shown in the following example:


    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    ...

For an intent to pass the category test, every category in the Intent must match a category in the filter.

Data test

To specify accepted intent data, an intent filter can declare zero or more  elements, as shown in the following example:


    <data android:mimeType="video/mpeg" android:scheme="http" ... />
    <data android:mimeType="audio/mpeg" android:scheme="http" ... />
    ...

Each  element can specify a URI structure and a data type (MIME media type).
Each part of the URI is a separate attribute: schemehostport, and path

<scheme>://<host>:<port>/<path>

The following example shows possible values for these attributes:

content://com.example.project:200/folder/subfolder/etc

In this URI, the scheme is content, the host is com.example.project, the port is 200, and the path is folder/subfolder/etc.

Advertisements

App chooser

When there is more than one app that responds to your implicit intent, the user can select which app to use and make that app the default choice for the action.

The ability to select a default is helpful when performing an action for which the user probably wants to use the same app every time, such as when opening a web page

If multiple apps can respond to the intent and the user might want to use a different app each time, you should explicitly show a chooser dialog.

The chooser dialog asks the user to select which app to use for the action (the user cannot select a default app for the action).

For example, when your app performs “share” with the ACTION_SEND action, users may want to share using a different app depending on their current situation, so you should always use the chooser dialog.

intent-chooser

To show the chooser, create an Intent using createChooser() and pass it to startActivity(), as shown in the following example.

Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);

// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}


Building an intent

 

Building an intent

Component name

  • The name of the component to start.
  • This is optional,
  • but it’s the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name.
  • Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category).

Action

A string that specifies the generic action to perform (such as view or pick).

Data

The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data
To set only the data URI, call setData().
To set only the MIME type, call setType().
If necessary, you can set both explicitly with setDataAndType().

Caution: If you want to set both the URI and MIME type, don’t call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.

Category

A string containing additional information about the kind of component that should handle the intent.
Any number of category descriptions can be placed in an intent, but most intents do not require a category.

Extras

Key-value pairs that carry additional information required to accomplish the requested action.

For example, when creating an intent to send an email with ACTION_SEND, you can specify the to recipient with the EXTRA_EMAIL key, and specify the subject with the EXTRA_SUBJECT key.

Note:

Do not use Parcelable or Serializable data when sending an intent that you expect another app to receive.

If an app attempts to access data in a Bundle object but does not have access to the parceled or serialized class, the system raises a RuntimeException.

Flags

Flags are defined in the Intent class that function as metadata for the intent.
The flags may instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it’s launched (for example, whether it belongs in the list of recent activities).

Implicit intent

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

For example, if you have content that you want the user to share with other people, create an intent with the ACTION_SEND action and add extras that specify the content to share. When you call startActivity() with that intent, the user can pick an app through which to share the content.

Use of resolveActivity():

It’s possible that a user won’t have any apps that handle the implicit intent you send to startActivity(). Or, an app may be inaccessible because of profile restrictions or settings put into place by an administrator. If that happens, the call fails and your app crashes. To verify that an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it’s safe to call startActivity(). If the result is null, do not use the intent and, if possible, you should disable the feature that issues the intent. The following example shows how to verify that the intent resolves to an activity. This example doesn’t use a URI, but the intent’s data type is declared to specify the content carried by the extras.

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

When startActivity() is called, the system examines all of the installed apps to determine which ones can handle this kind of intent (an intent with the ACTION_SEND action and that carries “text/plain” data).

If there’s only one app that can handle it, that app opens immediately and is given the intent.

If multiple activities accept the intent, the system displays a dialog such as the one shown in Figure , so the user can pick which app to use

intent-chooser

Explicit intent

  • An explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app.
  • To create an explicit intent, define the component name for the Intent object—all other intent properties are optional.
  • For example, if you built a service in your app, named DownloadService, designed to download a file from the web, you can start it with the following code:
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

 

What is Intent?

What is Intent?

An Android Intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver components.
startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

Types Of Intent?

There are two types of intents:

Explicit intents

  • specify the component to start by name (the fully-qualified class name).
  • You’ll typically use an explicit intent to start a component in your own app,
  • because you know the class name of the activity or service you want to start.
  • For example, you can start a new activity in response to a user action or start a service to download a file in the background.

Implicit intents

  • do not name a specific component,
  • but instead declare a general action to perform, which allows a component from another app to handle it.
  • For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

How Intent Works?

[1] Activity A creates an Intent with an action description and passes it to startActivity().

[2] The Android System searches all apps for an intent filter that matches the intent. When a match is found,

[3]the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.

intent-filters2x

You can find list of Common Intents here
https://developer.android.com/guide/components/intents-common.html