What are Intents in Android | Interception of Android Implicit Intents | A Complete Guide For Beginners

What are Intents in Android | Interception of Android Implicit Intents | A Complete Guide For Beginners

What are Intents in Android articles ACTE

About author

Anu Evanjiline (Mobile Application Developer )

Anu Evanjiline has extensive experience with MVP, MVVM, MVC, Rest API, Java, Android, iOS, B2C apps, fix bugs over a period of 5 years.Her articles assist in sharing information and abilities in core fields and provide students with informative knowledge

Last updated on 30th Dec 2021| 1243

(5.0) | 19857 Ratings

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose.

    • Introduction to intent
    • Intent types
    • Building an intent
    • Example explicit intent & implicit intent
    • Detect unsafe intent launches
    • Conclusion

    Introduction to intent:

    An Intent is a messaging object that you can use to request an action from another app component. Although intents facilitate communication between components in many ways, there are three fundamental use cases:


    Start 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(). Intent describes the activity to initiate and carry any required data.

    If you want to get the result when the activity is finished, call startActivityForResult(). Your Activity receives the result as a separate Intent object in your Activity’s onActivityResult() callback. For more information, see the Actions guide.


    Start a service

  • A service is a component that operates in the background without a user interface. With Android 5.0 (API level 21) and later, you can start a service with JobScheduler. For more information about JobScheduler, see its API-reference documentation.

  • For versions prior to Android 5.0 (API level 21), you can start the service by using the methods of the Service class. You can start a service to perform a one-time operation (such as downloading a file) by passing an intent to startService(). Intent describes the service to start and carries any required data.

  • If the service is designed with a client-server interface, you can bind to the service by passing an intent to bindService() from another component. See service guide for more information.

    • Subscribe For Free Demo

      [custom_views_post_title]

      Deliver broadcasts

      Broadcast is a message that any app can receive. The system distributes various broadcasts for system events, such as when the system boots up or the device starts charging. You can distribute broadcasts to other apps by passing an intent to sendBroadcast() or SendOrderedBroadcast().


      The rest of this page explains how intents work and how to use them. For related information, see Interacting with other apps and sharing content.


      Intent types:

      There are two types of intent:

      Explicit Intents- specify which application will fulfil the intent, either by supplying the target app’s package name or a fully qualified component class name. You would 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 within your app 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 rather declare a general action to be performed, which allows a component of another app to handle it. For example, if you want to show a location on a map to the user, you can use an implicit intent to request that another enabled app show a specified location on the map


    • Shows how an intent is used when starting an activity. When the Intent object explicitly names a specific Activity component, the system immediately initialises that component.

    • How an implicit intent is passed through the system to start another activity: [1] Activity A creates an intent with an action description and passes it to startActivity(). [2] The Android system searches all apps for intent filters that match the intent. When a match is found, [3] the system initiates a matching activity (Activity B) by invoking its onCreate() method and passing the intent to it.

    • When you use an implicit intent, the Android system looks for the appropriate component to start with, comparing the contents of the intent to intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers the intent object to it. If multiple intent filters are compatible, the system displays a dialog so the user can choose which app to use.

    • The manifest file of the intent filter app contains an expression that specifies the type of intent that the component wants to receive. For example, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain type of intent. Similarly, if you don’t declare an intent filter for an activity, it can only be started with an explicit intent.

      Building an intent:

      An Intent object contains information that the Android system uses to determine which component to initiate (such as the exact component name or component class that the Intent should receive), as well as information that the receiving component should properly execute. Uses to-to (such as to perform an operation on and to process data).


      The primary information contained in an intent is the following:

    • Component Name
    • Component name to begin with.

    • This is optional, but it is important information that specifies an intent, which means that the intent should only be given to an app component defined by the component name. Without a component name, the intent is implied and the system decides which component should receive the intent based on other intent information (such as action, data, and category—described below). If you need to start a specific component in your app, you should specify the name of the component.


      This field of intent is a ComponentName object, which you can specify using the target component’s fully qualified class name, which includes the package name of the app, for example, com.example.ExampleActivity. You can set the component name with setComponent(), setClass(), setClassName(), or with the intent constructor.


      Work

      A string that specifies the common action to be performed (such as view or select). In the case of broadcast intent, it is the action that took place and is being reported. The action largely determines how the rest of the intent is structured—specifically the information that is contained in the data and additions.

      You can specify your own functions for use by Intents within your app (or for use by other apps to invoke components in your app), but you can usually specify functions defined by the Intent class or other framework class. Specifies action constants. Here are some common actions to start an activity:


      ACTION_VIEW

      Use this action with startActivity() when you have information that can show the user an activity, such as a photo to view in the Gallery app, or an address to view in the Maps app.

      ACTION_SEND

    • Also known as a share intent, you should use it with startActivity() in an intent when you have some data that the user can share via another app, such as an email app or social sharing app.

    • See Intent class reference for more constants defining common actions. Other actions are defined elsewhere in the Android framework, such as actions in Settings that open specific screens in the system’s Settings app.

    • You can specify an action for an intent with setAction() or an intent constructor.

    • Statistics

    • URI (a Uri object) that refers to the data to be processed and/or the MIME type of that data. The type of data supplied is generally dictated by the action of the intent. For example, if the verb is ACTION_EDIT, the data must contain the URI of the document to edit.

    • When creating an intent, it is often important to specify the type of data (its MIME type) in addition to its URI. For example, an activity that is capable of displaying images probably won’t be able to play an audio file, even if the URI format is the same. Specifying the MIME type of your data helps the Android system find the best component to achieve your intent.

    • However, sometimes the MIME type can be inferred from the URI—especially when the data is a content: URI. A content: URI indicates that the data is located on the device and is handled by a content provider, which makes the data MIME type visible to the system. To set only the data URI, call setData() . To set only the MIME type, call setType() . If needed, you can set both explicitly with setDataAndType().

    • Category

      A string that contains additional information about the kind of component that should handle the intent. Any category description can be placed in an intent, but a category is not required for most purposes. Here are some common categories:


    • CATEGORY_BROWSABLE
    • The target activity itself allows a web browser to be initiated to display data referred to by a link, such as an image or an e-mail message.
    • CATEGORY_LAUNCHER
    • Activity is the initial activity of a task and is listed in the system’s application launcher.
    • You can specify a category with addCategory().
    • 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 decide which app component it should start. However, an intent may contain additional information that does not affect how it is resolved for an app component. An intent may also provide the following information:

    • Excessive

    • Key-value pairs that contain additional information needed to complete the requested operation. Just as some verbs use special types of data URIs, some verbs also use special additions.

    • You can add additional data with the various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the additional data, then put the Bundle into the Intent with putExtras().

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

    • The Intent class specifies a number of EXTRA_* constants for standardised data types. If you need to declare your own additional keys (for the intents your app receives), be sure to include your app’s package name as a prefix

    • Flags

      Flags are defined in the Intent class which serves as the metadata for the intent. Flags can instruct the Android system how to start an activity (for example, what tasks the activity should belong to) and how to treat it after it is launched (for example, whether it is in the list of recent activities) ). For more information, see the setFlags() method.


      Example explicit intent & implicit intent:

      An explicit intent is one you use to launch a specific app component, such as a particular activity or service in your app. To create explicit intents, define a component name for the intent object—all other intent properties are optional. For example, if you’ve created a service named DownloadService in your app, which is designed to download a file from the web, you can start it with the following code:


      • // executed in an activity, so ‘this’ is the context
      • // fileUrl is a string URL, such as “http://www.example.com/image.png”
      • val DownloadIntent = Intent(this, DownloadService::Class.java). apply the {
      • data = Uri.parse(fileUrl)
      • ,

      start service (download intent)

      The Intent(context, Class) constructor supplies the Class object to the AppContext and Component. Thus, this intent explicitly starts the DownloadService class in the app. For more information about creating and starting a service, see the Service Guide.


      Example implicit intent:

      An implicit intent specifies an action that any app on the device capable of performing the action can invoke. Using an implicit intent is useful when your app can’t perform the action, but other apps probably can and you want the user to choose which app to use.

      For example, if you have content that you want users to share with other people, create an intent with the ACTION_SEND action and add additional ones specifying the content to share. When you call startActivity() from that intent, the user can choose an app through which to share content.


      • // Create text messages with a string.
      • val sendIntent = Intent(). apply the {
      • Action = Intent. ACTION_SEND
      • putExtra(Intent.EXTRA_TEXT, textMessage)
      • type = “text/plain”
      • ,
      • // Try to implement the intent.
      • Try {
      • startActivity(send intent)
      • } catch (e: ActivityNotFoundException) {
      • // Define what your app should do if an activity cannot handle the intent.
      • ,

      When startActivity() is called, the system checks all installed apps to determine which can handle such intent (an intent with ACTION_SEND action and containing “text/plain” data ). If there is only one app that can handle this, that app is immediately opened and the intent is given. If no other app can handle it, your app may catch ActivityNotFoundException.


      Forcing an app chooser:

      When there is more than one app that responds to your implicit intent, the user can choose 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 that requires the user to use the same app each time, such as when opening a web page (users often prefer just one web browser).


      However, if multiple apps can respond to the intent and the user wants to use a different app each time, you should explicitly show a selector dialog. The selector dialog asks the user to choose which app to use for the action (the user cannot select the default app for the action). For example, when your app “shares” with the ACTION_SEND verb, users may want to share using a different app based on their current state, so you should always use the selector dialog, as in the picture. Shown in 2.


      To show the selector, create an Intent using createChooser() and pass it to startActivity(), as shown in the following example. This example displays a dialog with a list of apps that respond to an intent passed to the createChooser() method and use the supplied text as the dialog title.


      • val SendIntent = Intent(Intent.ACTION_SEND)
      • // Always use string resources for UI text.
      • // it says something like “Share this photo with”
      • val title: String = Resources.getString(R.string.chooser_title)
      • // Create intent to show selector dialog
      • val selector: intent = Intent.createChooser(sendIntent, title)
      • // Verify that the original intent will resolve at least one activity
      • if (sendIntent.resolveActivity(packageManager) != null) {
      • start activity (selector)

    Course Curriculum

    Develop Your Skills with Advanced Android Certification Training

    Weekday / Weekend BatchesSee Batch Details

      Detect unsafe intent launches:

      Your app may launch with the intention of navigating between components inside your app, or to perform an action on behalf of another app. To improve platform security, Android 12 (API level 31) and higher provide a debugging feature that warns you if your app launches an Intent Unsafe. For example, your app may have an unsafe launch of a nested intent, which is an intent that is passed as an addition to another intent.

      If your app performs both of the following actions, the system detects an unsafe intent launch, and a StrictMode violation occurs:


    • Your app separates nested intents from the addition of delivered intents.
    • Your app immediately starts an app component using that nested intent, such as passing the intent to startActivity(), startService(), or bindService().
    • For more information on how to identify this situation and make changes to your app, read the blog post about Android Nesting Intents on Medium.

    Android Sample Testing Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Conclusion:

      In Android, it is quite common for users to jump from one application to another as part of the whole process, for example, searching for a location on the browser and seeing a jump directly into Google Maps or receiving payment links in messages. On clicking the application (SMS) and PayPal or GPAY (Google Pay). This process of moving users from one application to another is achieved by passing intents to the system. Intents, in general, are used to navigate between different activities within the same application, but note, are not limited to a single application, i.e., they can also be used by moving from one application to another.

      Intents can be implicit, for example, to call desired actions as well as explicitly, such as opening another activity after some operation like onclick or something. Some of the applications of intent are given below:


    • Send user to another app
    • Get results from an activity
    • Allowing other apps to start your activity

    Name Date Details

    14-Oct-2024

    (Mon-Fri) Weekdays Regular

    16-Oct-2024

    (Mon-Fri) Weekdays Regular

    12-Oct-2024

    (Sat,Sun) Weekend Regular

    12-Oct-2024

    (Sat,Sun) Weekend Fasttrack