lilt: Lightweight Interactive Learning Tool

Topic 12

Implicit Intents

Implicit intents allow us to request the services of another application from our own.

  • Why is this useful?
    • We might want to make a phone call, send an email, take a photo or look at a web page from within our app (e.g. in a points of interest app, we might want to allow a user to call or message a pub or hotel)
    • Rather than implementing email, photo or web functionality ourselves, we can launch one of the standard system apps from our own
    • We do this by sending an implicit intent to launch the default application for a particular action

Sending an implicit intent

  • Here is an example of an implicit intent. Note that apply() is similar to let(), but we refer to the subject of the call (the Intent) within the apply() block using this, rather than it. In fact this can be omitted to make the code more concise, as is the case below. We are using apply() ragther than let() as apply() automatically returns the subject of the apply() block which means we can assign it to the variable intent.

  • This is an intent to set the alarm for 04:30
  • It uses whichever app is registered as the default alarm clock app
  • Note the use of an inbuilt action, AlarmClock.ACTION_SET_ALARM
  • We add the details of the alarm to the Intent as extras (similar to the approach you have seen before for passing information through an Intent)
  • Note also how we have to check that there is an activity capable of receiving this intent:

Another example - opening the phone dialer

  • Using the inbuilt action Intent.ACTION_DIAL, this example will launch the phone dialer with a specific phone number loaded in
  • With dial intents, we set the intent's _data_ rather than adding extras
  • e.g:

A third example - opening a web page

The ACTION_VIEW implicit intent will open a web browser to view a given URL. Again the URL is set as the intent's data.

What if more than one app recognises an implicit intent?

  • It's possible that more than one activity on the device will respond to a given implicit intent, e.g. main activities for two web browsers or email apps
  • The user can select a default app, e.g. default web browser or email app
  • If you want to give the user the choice each time, however, you should launch a chooser to allow the user to select the preferred app
  • e.g. this example allows the user to pick a browser app (this example is based on that given in the Android documentation):

Making your apps respond to implicit intents

  • As well as launching other apps using implicit intents, you might wish to make your own apps respond to implicit intents, so that other apps can call them
  • To do this you create an intent filter (you've already seen these in the broadcasts topic) to allow your activity to receive selected implicit intents
  • In this case, intent filters are specified in your manifest:

  • Note how we have placed the intent-filter tag inside the activity tag
  • Within the intent-filter tag we specify the action to allow through and the category of intent (usually you can leave this as DEFAULT as here)

Reading an implicit intent received by an activity

  • Having set up our intent filter, we can read the received implicit intent from our activity with the intent attribute (a readable property, in Java you would use getIntent()
  • We can then check the intent's action (the intent filter might be allowing more than one intent type through), and then read its extras
  • Or, if we passed data through the intent instead (as in the phone dialer example), we can read it with getData()
  • To send this implicit intent from another activity, we'd simply create an intent with an action of MyAction

Passing back data from an activity launched with an implicit intent

  • An activity launched with an implicit intent can pass back an Intent to whatever called it with setResult(), and then call finish() (as you have seen already with activities launched with regular Intents)
  • You can use the same techniques to launch an activity with an implicit intent, as you can for a regular (explicit) intent

Resources

Exercise

Change your main activity from the Mapping topic (topic 4) so that it can receive an implicit intent from another application. The implicit intent should have an action of ACTION_DISPLAY_MAP and should take latitude and longitude as extras. The activity should read the latitude and longitude from the implicit intent and set the map's location accordingly. Write a second application (in a separate project) including an activity to read in the latitude and longitude from the user and send them over to the mapping activity.