Home

Blog

Java

Eclipse

Android

Google

Web

Technology

Algorithms

Training

You can help me maintaining this article:

Android Intents - Tutorial

Lars Vogel

Version 0.4

Copyright © 2009 - 2010 Lars Vogel

22.12.2010

Revision History

Revision 0.1

17.07.2010

Lars Vogel

Created

Revision 0.2 - 0.4

19.07.2010 - 22.12.2010

Lars Vogel

bug fixes and enhancements

Using Intents in Android

This article describes how to use intents in Android. It is
based on Eclipse 3.6, Java 1.6 and Android 2.3
(Gingerbread).

Table of Contents

1. Android Intents

1.1. Android Basics

1.2. Intents

2. Implicit Intents - Opening an URL

3. Explicit intents - Example

4. Registering via Intentfilter

5. Finding out if an intent is available

6. Thank you

7. Questions and Discussion

8. Links and Literature

8.1. Source Code

8.2. Android Resources

8.3. vogella Resources

1. Android Intents

1.1. Android Basics

The following assumes that you have already basic knowledge in
Android development
. Please check the
Android development tutorial
to learn the basics.

1.2. Intents

Intents are asynchronous messages which allow the application to send or receive data
from and to other activities or services. For
example the application call ask via an intent for a
contact
application. Application register themself via an
IntentFilter.
Intents are a powerful concept as they allow to create
loosely
coupled applications.

"android.content.Intent" is an object. This object can contain
information for the receiving
component. For example if your
application calls via an intent a
browser it may send the URL to the
browser component. An intent also contain information for the Android
system so that the Android system can determine which component
should handle the request.

Android supports explicit intents and implicit intents.
Explicit intent name the component, e.g. the Java class which should
be called. Implicit intents asked the system ot perform a service,
e.g. to open a browswer. The Android system will determine suitable
application and if serveral applications exists offer the user the
choice to open one. The determination is based on intent filters,
e.g. the class "android.content.IntentFilter". Intent filters are
typically defined via the "AndroidManifest.xml" file. A
component uses
intent filter to define for which intents it can be
responsible. If a
component does not define intent filters it can
only be caeed by
explicit intents.

2. Implicit Intents - Opening an URL

The following is an example for in implicit intent. The Android
system is asked to display a URI and chooses the browser for this
task. Create a new Android application
"de.vogella.android.intent.browser" with the Activity "OpenBrowser".
Create the following view layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open Browser" android:onClick="openBrowser"></Button>
</LinearLayout>

Use this class to open the Browser

package de.vogella.android.intent.browser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class OpenBrowser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void openBrowser(View view) {
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.vogella.de"));
startActivity(i);
}
}

3. Explicit intents - Example

The following demonstrates an explicit intent, two activities
will call each other via intents. Create a new Android application
"de.vogella.android.intent.explicit" with the Activity "ActivityOne".
Create the following view layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text1:"></TextView><EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText></LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text2:"></TextView><EditText android:text="@+id/EditText02" android:id="@+id/EditText02" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText></LinearLayout>
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Calling an intent"></Button>
</LinearLayout>

Create a new activity "ActivityTwo" via the AndroidManifest.
Create
the following coding for your two activities. These activities
will
call each other, transferring data for the "putExtra()" method of
the t class.

package de.vogella.android.intent.explicit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class ActivityOne extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras== null){
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 !=null){
EditText text1= (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
}

public void onClick(View view){
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
}
}

package de.vogella.android.intent.explicit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class ActivityTwo extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle bundle ) {
super.onCreate(bundle);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras== null){
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 !=null){
EditText text1= (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
}

public void onClick(View view){
Intent i = new Intent(this, ActivityOne.class);
i.putExtra("Value1", "Swinging on a star. ");
i.putExtra("Value2", "You could be bettern then you are. ");
startActivity(i);
}
}

4. Registering via Intentfilter

Your application can also register ifself for implicit intents
in
Eclipse. For this you have to specify an intent filter for the
selected event. Intent filters are typically declared in
"AndroidManifest.xml". An intent filter must specify category, action
and data filters.

Lets create a small browser. Our application register itself as
a browser and will display a webpage as the pure HTML code. Create the
Android project "de.vogella.android.intent.browserfilter" with the
activity "BrowserActivitiy". Change "AndroidManifest.mf" to the
following to register your application to the browser view intent. THe
following also request the permissioni to access the internet.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.intent.browserfilter"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BrowserActivitiy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="9" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

As your activity gets called with an intent you can get the data
from the intent and display it in your application.

package de.vogella.android.intent.browserfilter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class BrowserActivitiy extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
TextView text = (TextView) findViewById(R.id.textView);
// To get the action of the intent use
System.out.println(intent.getAction());
// To get the data use
Uri data = intent.getData();
URL url;
try {
// url = new URL(data.getScheme(), data.getHost(), data.getPath());
url = new URL("http://www.vogella.de");
BufferedReader rd = new BufferedReader(new InputStreamReader(
url.openStream()));
String line = "";
while ((line = rd.readLine()) != null) {
text.append(line);
}

} catch (Exception e) {
e.printStackTrace();
}

}
}

If you call and URL you should be able to select your browser and
the HTML code should be loaded into your text view.

5. Finding out if an intent is available

Sometimes you want to find if an application is available for a
certain intent. This can be done via checking the PackageManager. The
following code checks if an intent exists. You can check via this
method for intent and change your application behavior accordingly for
example disable or hide menu items. This tip is based on a
blog entry from Romain Guy

.

public boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> resolveInfo =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) {
return true;
}
return false;
}

6. Thank you

Please help me maintaining this article:

7. Questions and Discussion

Before posting questions, please see the
vogella FAQ
. If you have questions or find an error in this article please
use the
www.vogella.de Google Group
. I have created a short list
how to create good questions
which might also help you.

8. Links and Literature

8.1. Source Code

Source Code of Examples

8.2. Android Resources

Android Location API and Google Maps

Android and Networking

Android Homepage

Android Issues / Bugs

Android Google Groups

8.3. vogella Resources

Eclipse RCP Training Join my Eclipse RCP Training to become an RCP Expert in 5 days (Training in German)

Android Tutorial
Introduction to Android Programming

GWT Tutorial
Program in Java and compile to JavaScript and HTML

Eclipse RCP Tutorial
Create native applications in Java

JUnit Tutorial
Test your application

Git Tutorial
Put everything you have under distributed version control system