Android Intents

Intents :

 

Intents are used to navigate  between two activities within the application means same app or outside the application means from the other activity within same the device outside the app. It is more important to know more about Intents in Android.

Intent Starting an activity :

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to  startActivity(). It starts new activity and displays it.

To view  result from the activity when it finishes, It call startActivityForResult() call back method and receives the result as a separat  Intent object in your activity’s  onActivityResult() callback method

Intent Starting a service :

A Service is a component that performs operations in the background without a user interface.

Intent types :

There are two types of intents:

  • IntentsExplicit :
  • are used to start a new activity within the app. You can 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 might start a new activity within your app in response to a user action, or start a service to download a file in the background etc.
  • ****************
  • Note:An explicit intent is always delivered to its target, regardless of any intent filters the component declares.
  • ********************
  • Implicit intents:
  • Implicit Intents are used to start the new Activity of the other app within the device. do not name a specific component, but instead declare a general action to perform, that 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 to show a specified location on a map.

The below Figure 1 demonstrates  how an intent is used when starting an activity.

 

In above figure 1 it requests for Implicit intents  request to the android system or android device in figure 2.  then Android System searches for that specified intents in  android systems . Once it found by the android Systems it will starts that activity immediately by  calling onCreate() call back method in figure 3.

We have to  declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. If we do not declare intent filters for other apps it will start as  an explicit intent.

 

Building an intent  :

An Intent object carries information that the Android system uses to determine which activity to start in order to properly perform the action

Component name :

The name of the component to start. 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). You can specify your own actions for use by intents within your app, but you usually specify action constants defined by the  Intent class or other framework classes. below is some common actions for starting an activity:

ACTION_VIEW :

With this action in an intent with  startActivity().   We see photo to view in a gallery app, or an address to view in a map app , address, etc.

ACTION_SEND :

action send intent used to share data with other others outside the app

If you want to define your own actions define by using your app’s package name as a prefix, as shown in the following example below:

staticfinalStringACTION_TIMETRAVEL="com.example.action.TIMETRAVEL"

Category :

An intent filter can declare as zero or more <category> elements for intent categories. The category is defined in the name attribute, and consists of the string "android.intent.category.” plus the name of the intent category,

For example below sample code used to declate them in manifest file, this intent filter matches either CATEGORY_DEFAULT and CATEGORY_BROWSABLE and starts the activity:

<intent-filter>

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

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

</intent-filter>

Note that all activities that you want to accept implicit intents must include the android.intent.category.DEFAULT intent-filter. This category is applied to all implicit Intent objects by the Android system.

Intents are placed in categories . number of category descriptions can be placed in an intent, but there are some  intents they do not require a category. Here are some common categories:

CATEGORY_BROWSABLE  :

The target activity allows itself to be started by a web browser to display data referenced by a link, such as an image or an e-mail message.

CATEGORY_LAUNCHER  :

The activity is the initial activity of a task and is listed in the system’s application launcher.

The below example describes  an Activity with an Intent filter to receive an ACTION_SEND data

<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>

These properties listed above (component name, action, data, and category) represent the defining characteristics of an intent. By reading these properties, the Android system is able to resolve which app component it should start.

Data

An intent filter can declare zero or more <data> elements for the URI contained in the intent data. As the intent data consists of a URI and MIME type optionally, you can create an intent filter for various aspects of that data, including:

  • URI Scheme
  • URI Host
  • URI Path
  • Mime type

For example, the intent filter matches data intents given in intent filters with a URI scheme of http and a MIME type of either “video/mpeg” or “audio/mpeg”.

<intent-filter>

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

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

...

</intent-filter>

 

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

Intent flags are options used to specify how the activity receives the intent should handle that intent. Intent flags are defined as constants in the Intent class and begin with the word FLAG_. You can add intent flags to an Intent object with the help of setFlag() or addFlag().

There are three specific intent flags are used to control activity launch modes, either in conjunction with the launchMode attribute or in place of it. Intent flags always take precedence over the launch mode if it has any conflict.

  • FLAG_ACTIVITY_NEW_TASK :it is used to start the new activity.
  • FLAG_ACTIVITY_SINGLE_TOP : if the activity to be launched is at the top of the back stack, route the intent to that existing activity instance. Otherwise it create a new activity instance.
  • FLAG_ACTIVITY_CLEAR_TOP : If the activity to be launched already exists in the back stack, destroy any other activities on top of it and, this flag locates any existing instances of the activity in any task and brings it to the foreground.

Intent Implicit Example :

An implicit intent used to perform  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  run as a user select which one and what type of app user want to use.

// 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 on the device and searches for the app which one handles this type of request if it is one then it starts immediately. if there are more than one that matches this request it displays all the activities list in a dialogue. and displays the user on the device and user selects it.

Intent Explicit Example :

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,

The below example demonstrates how to use explicit intent to start DownloadService

// Executed in an Activity, so 'this' is the  Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData( Uri.parse(fileUrl));
startService(downloadIntent);

The The Intent(Context, Class)  constructor supplies the app  Context and the component as a  Class object. As such, this intent explicitly starts the DownloadService within the app.

 

When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.

Receiving an implicit intent :

To advertise or display other apps on your device  which implicit intents your app can receive, declare all of them in the  <intent-filter>; element in your  manifest file. Each intent filter specifies the type of intents it accepts based on the intent’s action, data, and category. The system delivers an implicit intent to your app component only if the intent can pass through one of your intent filters.

An app component should declare separate filters for each unique job to perform in  it. For example, one activity in an image gallery app may have two filters: one filter to view an image, and another filter to edit an image.

All the  intent filter is defined by an   <intent-filter> element in the app’s manifest file, nested in the corresponding app component (such as an <activity> element). Inside the  <intent-filter>.

Below Example demonstrates how to use intent filters in manifest file of your app:

<activity android:name="MainActivity">
<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
</activity>

The first activity, MainActivity, is the app’s main entry point to start Activity when app launches

  • The  ACTION_MAIN action indicates this is the main entry point of an app and Intents are not allowed here
  • The  CATEGORY_LAUNCHER category indicates that this activity’s icon should be placed in the system’s app launcher by default. If the <activity> if we don’t specify an element an icon with icon, then the system uses the icon from the  <application> element by default.

These two must be paired together in order for the activity to appear in the app launcher.

The second activity,  ShareActivity, is intended to facilitate sharing text and media content. Using this intent user can migrate or Navigate to  ShareActivity from  MainActivity of the app, User  can also enter   ShareActivity directly from another app that depends on the user’s decision. Means user can migrate from main activity to another activity or He can open directly the new ShareActivity directly in his device.

Congratulations You Have Learned About Intents. I Have Explained Explict Intent Example In Next Post. If You Want To Go With Example Click Here Ecplicit Intent Example 

Leave a Reply

Categories