Android Intent – Part 1

It is one of the most important topics in Android, which you will be using repeatedly on a day to day development. So what is Android Intent? Not that difficult to guess right? The name “Intent” says it all i.e. It is a way of telling the system what your intentions are.

An Android Intent is an Intent object that holds the content of the message. In android activities, services and broadcast receivers are activated by asynchronous messages called intents.

  • An Activity is launched by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The corresponding activity which got activated from can read the activating intent object( an object which started this activity) by calling its getIntent().
  • For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things.
    • For example, the intent might tell activity to show some image to the user.
      • Intent intent=new Intent();Intent.putAction(Intent.ACTION_SEND);Intent.putType(“Image/JPEG”);

        startActivity(intent).

  • For Broadcast Receivers, the intent object names the action being announced
    • For example it can announce other interested components(services, activities etc) that some data has been downloaded.
    • An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(),Context.sendOrderedBroadcast() etc.
    • Android delivers the intent to all interested broadcast receivers by calling their onReceive().
  • For Services, Intents are used to start a service or new instructions by passing an Intent object to Context.startService();
    • Android calls the service’s onStart() and passes it the intent object.
    • Intent can be passed Context.bindService() to establish an ongoing connection between the calling component and a target service
    • The service receives the intent object in onBind() call
      • In case service is not already running,bindService() can optionally start it.
    • An Activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means(a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.

Intents can be classified into two types:

  • Explicit Intents
  • Implicit Intents

Let’s understand it one by one :

Explicit Intents are those intents in which you clearly tell the Android system what you want to do. For example, you are in Activity A and you tell the system that you want to open Activity B. Mostly you use an explicit intent for opening or starting a component within your own app.

Implicit Intents are those intents in which you do not exactly know what component you want to open but you have an idea about what to do. For example, if you want to open a PDF document and your app do not provide that functionality, you can make use of implicit intent and request to a system that another capable app, can open this PDF.

Now, you might be thinking how is this possible? So let’s see what happens when we create an implicit intent:

  • Implicit intent is handled by the Android system
  • The content of this implicit intent is compared with the intent filters declared in the manifest file of the other apps installed in the system.
  • If the match is found, the system starts the component and delivers it to intent object.
  • If multiple match is found, the system opens a dialog so that user can pick which app to use.

It is very important to understand the structure of an intent object since in android everything revolves around intents and its usage in a different use case. So an intent object contains following important information:

  • Component Name
    • This is the most important piece of information, although optional this information determines whether the intent object would be explicit(on basis of the component name given to the intent object) or implicit(on basis of action, data, and category)
  • Action
    • This information specifies what action is to be performed.
    • When used with broadcast, it signifies what action has taken place.
    • Sometimes when we start an activity we make use of some common Intent Actions such as ACTION_VIEW, ACTION_SEND
  • Data
    • This piece of information refers to the data on which action is to be performed. It might be the MIME type of that data. More often the ACTION and DATA information are co-related. For example, if the Action is ACTION_VIEW then data associated with it would be the URI of the resource to be viewed.
    • URI is uniform resource identifier and contains a string of characters used to identify a resource either by location or name or both of them.
    • In addition to URI, it is always important to specify the MIME type of that data.This is decisive information which helps android system to identify the correct type of application to open the resource because an activity which might display image might not play audio file although URI for both can be same.
    • When data is a content: URI, which signifies that data is located on the device and controlled by ContentProvider which makes MIME type visible to the system.
  • Category
    • This piece of information specifies what kind of data the component can handle.
    • You can add multiple numbers of category inside an intent.
    • Some of the common categories are: CATEGORY_LAUNCHER, CATEGORY_DEFAULT
  • Extras
    • This piece of information contains additional data in form of Key-Value pair in order to perform any specific task. You can also create a bundle object with all the extra data, then insert the Bundle in the intent with putExtras().
  • Flags
    • The described behavior for activities and tasks can be customized by specifying certain intent flags when starting an activity. For example, if you want to bring an Activity on top of the stack which is already existing in the task stack but not by creating a new instance.

We would look into more detail in the coming post about how we can customize our activity launching with the help of these flags. So stay tuned

1 thought on “Android Intent – Part 1”

Leave a Reply

Your email address will not be published. Required fields are marked *