Intents: The Glue That Binds - New Format

No description »
Matt Oakes

Intents
The Glue That Binds
Matt Oakes
Twitter: @matto1990
Email: matt@matto1990.com
www.matto1990.com
What Are Intents?
From developer documentation:
Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. Intent messaging is a facility for late run-time binding between components in the same or different applications. The intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed — or, often in the case of broadcasts, a description of something that has happened and is being announced.
Ok...
But what does that really mean?
Used in activities to open other activities
Can open activities in other applications
And to send broadcasts
final Intent intent = new Intent(Intent.ACTION_VIEW, Activity.class);
startActivity(intent);
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 2);
final Intent intent = new Intent(SyncService.ACTION_STATUS, null, context, SyncService.class);
startService(intent);
final Intent intent = new Intent("com.matto1190.app.madlab.BROARDCAST_STATUS");
intent.putExtra(STATUS, STATUS_OK);
sendBroardcast(intent);
They tie all the stuff in an application together
You application is (almost) useless without them!
How do they work?
Differnet types:
Explicitly choosing an activity (or other componant)
Giving a general intent which can be handled by any application which tells the system it can
Mainly used when you know only you can serve the Intent
Explicit Intents
General Intents
Used mainly when you can't handle the request yourself
Normally only open activities within the same application
You need to know the exact class name and have it visiable
Almost all applications will use this at some point
Unless your application only has one activity I guess
Such as moving between activities in an application
public void onClick(View button) {
  final Intent intent = new Intent(Intent.ACTION_VIEW, Activity.class);
  startActivity(intent);
}
Can be used to open any applications on the system
If more than one application can handle the request the user will be given a choice
The user can select a default if they wish
Application registers ability to serve requests
This is done in the manifest
As long as an application states it can serve your request
Or to give the user a choice in which application is used
Ok...
So how do we do it?!?!
In our application...
String url = "http://madlab.org.uk/";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
What the user sees...
We're going to open a web page
We don't want to deal with this ourself
We want it to open in the users web browser
How do the system know
which browsers exist?
Using intent filters
In the AndroidManifest.xml
<activity android:name="BrowserActivity" android:label="@string/application_name">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" />
    <data android:scheme="https" />
  </intent-filter>
</activity>
Can you think of a place that 99% of all android apps use Intent Filters?
Opening the application from the launcher
<activity android:name=".HomeActivity" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
So what have we learnt?
Your application can't do much without them
They are used in every single application; both in the system and apps made by 3rd party developers
You need to understand them to make good use of them
They are the glue which bind all the bits of you application together
Student at University of Manchester
Currently in 2nd year of Computer Science
But there's more!
There are many thing that are possible which developers don't do
Define a set of intents for all apps built around a service
Creating a set of common intents
The ultimate example...
Twitter!!!
Imagine this...
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("twitter://tweets?user=android_mcr"));
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("twitter://post?msg=A Message"));
startActivity(intent);
Or this...
If the user has more than one Twitter application they would get a choice of which one to use
They could even set a default
This is all possible now. It just takes the developers getting together to make it actually happen
Obviously...
This isn't just useful for Twitter apps
I could be for anything!!!
Facebook
Foursquare
App Markets
Any social service really!
But many developers don't :'(
And another thing!
You can push intents to a device from a server
Using C2DM
Google API to push intents to a phone
Register an app on the phone to recieve the push messages
Confirm on the server end
The server can now push a message to the phone
Phone gets the message and the app can deal with it
Cool!
What could I use that for??
Twitter client which pushes updates
No need for polling
Instant (nearly) notifications about DM's or mentions
Push Email
How do you think Gmail has worked all this time?
Telling an application to download something
Download a new application
Download a new song
Download a podcast the moment is is released
Can also be used to open services
Demo Time
This is how content providers work
Unfortunately, that's a massive talk in itself

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Matt Oakes