Intent and Intent Filters

 

A intent is an abstract description of an action to be performed. Also known as a messaging component that is used to request certain action from another component.

Intents have basic fundamental use cases:

  1. To start an activity: Used to start new instance of an activity by passing intent to it.
    ie. startActivity(new Intent(<CONTEXT_PACKAGE>, <CLASS>)).
    Similarly, if we want to receive a result from the starting activity then we use startActivityForResult(<INTENT>, <RESULT_CODE>).
  2. To start a service: Start a service to perform one time service by passing intent to startService(<INTENT>).
  3. To deliver a broadcast: Deliver broadcast to other apps by passing intent on functions such as sendBroadcast(<INTENT>). There are other ways to send broadcasts as well which will help differentiate what type of broadcast is being delivered. Learn more here.

Intent Types

There are two types of intents

  1. Explicit Intent: We use explicit intents when we can specify the component to start the intent explicitly. We usually used explicit intent within our app. Typically, when you open an activity or start a service by typing the name of the component (class) itself then it is called explicit intent.
  2. Implicit Intent: When we don’t declare explicit component and instead specify action to be performed, which allows component from other apps to handle it.
    Whenever an implicit intent is used the android system is notified to show all the available components in our mobile that can handle that action. Eg: send mail, make call etcs

Intent Fields

  1. Action
    A string that specifies the generic action that is to be performed.
    eg: ACTION_DIAL : Dial a number,
    ACTION_EDIT,
    ACTION_SYNC,

    ACTION_MAIN: Start an activity as an initial activity
    We can specify our own actions for use by intents within your app.
  2. Data and/or Type
    The URI that references the data that is to be acted on and/or also defines the MIME type of the data. setData() ,setType() or setDataAndType().
    Note : If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.  
  3. Category
    Additional information of the component that can handle the intent.
  4. Extra
    Key-Value pairs that contains additional information that is to be passed  required to accomplish the requested action. According to the need we can add extras that can be String, boolean, integer, array list etcs.
  5. Component
    Component is the class that should receive this intent. Use this when there is exactly one component that should be received
  6. Flags
    The flags may instruct the Android system how to launch an activity and how to treat it after it’s launched.
    eg: FLAG_DEBUG_LOG_RESOLUTION: put extra debug logging information when intent is being processed.

Intent Filters

Android filters through activities, services and broadcast receivers in the system to that handles the specified intent with the help of its fields such as action, category etcs. These intent filters are defined in manifest with tag <intent-filter>.

Eg:

<activity android:name=".WebViewActivity"
   android:label="@string/app_name">
   
   <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <action android:name="com.example.My Application.LAUNCH" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:scheme="https://techtrendzy.wordpress.com" />
   </intent-filter>
   
</activity>

 

Follow this link for sample code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s