Android Core - Android Tutorial

Home Android Tutorials Android News Contact Us
Working with GData APIs:
Youtube on Android?
10 Open Source Android Apps
which every Android developer
must look into
More Animations, Part 2
Latest News
How to Install the Android SDK
on Windows XP
Making a custom Android
button using a custom view
Flipping Your Views
Popular
Login
Android Core Menu
Android News
Android Tutorials
Android Books
Android Videos
Android Resources
Android Download
Members
Username
Password
Remember Me
Forgot your password?
Forgot your username?
Create an account
Syndication
Subscribe to this Feed
Android Applications Tutorial: Intent
13.0 Intent
13.1 Explicit Intent
13.2 Implicit Intent
13.3 Launching a Peer Activity
13.4 Intent Tabs
13.0. Intent
Intent is basically a message that is passed between components (such as Activities
Receivers, and Content Providers). So, it is almost equivalent to parameters passed to API calls. The
fundamental differences between API calls and intents' way of invoking components are:
API calls are synchronous while intent-based invocations are asynchronous.
API calls are compile time binding while intent-based calls are run-time binding
Of course, Intents can be made to work exactly like API calls by using what are called
will be explained later. But more often than not, implicit intents are the way to go
here.
One component that wants to invoke another has to only express its' intent to do a job. And any other
component that exists and has claimed that it can do such a job through intent-filters
android platform to accomplish the job. This means, both the components are not aware of each other's
existence and can still work together to give the desired result for the end-user.
This invisible connection between components is achieved through the combination
the android platform.
This leads to huge possibilities like:
Mix and match or rather plug and play of components at runtime.
Replacing the inbuilt android applications with custom developed applications.
Component level reuse within and across applications.
Service orientation to the most granular level, if I may say.
Here is additional description about intent, almost formal.
An intent is an abstract description of an operation to be performed. It can be used with
an Activity, broadcastIntent to send it to any interested BroadcastReceiver
startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background
Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its
Home Android Tutorials Android Applications Tutorial: Intent
ª--~ :---- م>-أ
'-'=- '+'--=-و '+--ه'--' ن`ا ك·=-- »''·'ا ª-'+- تا·-- ل·= ا-= ª-'هو
www.Inzar.com/ArabicVideos
Android World of Dragons
Is a free Mobile MMORPG Game for Android.
Download & Play!
www.playandroid.com/WorldofDragons
Programming
The latest & greatest
in parallel computing.
Visit our expert blog!
www.drdobbs.com/Go-Parallel
Eclipse J2EE IDE
Comprehensive
Solution & Support
Only $29.95 -
Download Free Trial
www.myeclipseide.com
Android Market Apps
Find your favorite Android apps from the
Android Market!
www.androidpit.com/market
OMAP MID Platform
MID Solution based on TI OMAP3530 Supports
for Android and WinCE
www.armkits.com
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 1/11
Latest Comments
ashikch
Bounce Animation
some error!! - I understood the
code ...
Aneev
How to Install the A...
Aneev
10 Open Source Andro...
how to compile a code on android.
tel...
JOHNSInez
Compile Android kern...
reply this topic - We encourage
you y...
EvangelineVincent33
How to implement you...
re - All people deserve wealthy life
...
An Intent provides a facility for performing late runtime binding between the code in different applications. Its
most significant use is in the launching of activities, where it can be thought of as the glue between activities. It
is basically a passive data structure holding an abstract description of an action to be performed. The primary
pieces of information in an intent are:
action
The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN,
data
The data to operate on, such as a person record in the contacts database, expressed as a Uri.
All Android components that wish to be notified via intents should declare intent filters so that Android knows
which intents should go to that component. So, we need to add intent-filter elements to our
AndroidManifest.xml file. It looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package="com.bogotobogo.myContacts"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".myContacts"
android:label="@string/app_name">
<intent-filter>
<action android:name=
"android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />

<uses-permission android:name=
"android.permission.READ_CONTACTS"/>
</manifest>
Note that intent-filter element is under the activity element. In the file, we are declaring that this activity is
(1) the main activity for this application and (2) the activity is in the LAUNCHER category, meaning
icon in the Android menu. Because this activity is the main one for the application, Android knows this is the
component it should launch when someone chooses the application from the main menu.
Once we have our intent, we need to pass it to Android and get the child activity to launch. Here, we have two
options:
Call startActivity() with the Intent. This will cause Android to find the best matching activity and pass the
intent to the activity for handling. The activity will not be informed when the child activity
Call startActivityForResult(), passing it the intent and a number which is unique to the calling activity.
Android will find the best matching activity and pass the intent over to the activity. The
when the child activity is complete via onActivityResult() callback.

13.1 Explicit Intent
In an explicit intent, we actually specify the activity that is required to respond to the intent. In other words,
we explicitly designate the target component. This is typically used for application internal messages.
In an implicit intent, the main power of the android design, we just declare an intent and leave it to the
platform to find an activity that can respond to the intent. Here, we do not declare the target component and
hence is typically used for activating components of other applications seamlessly
Let's look at our example:
This example has 2 activities:
InvokingActivity
InvokedActivity
The InvokingActivity has a button "Invoke Next Activity" which when clicked explicitly calls the
InvokedActivity class. The relevant part of the code is here:
Android
Notification SDK
Developers Add
Notifications In <10
Min. Fast, Flexible &
Free SDK
hub.buzzbox.com
Web/XML Appl.
Generator
NARM/Network
Application Rendering
Model
www.bitech-factory.ch
Killer Android
Tweet App
Killer App with new
Live Preview That
Shows Links In
Tweets-Free
www.Twidroyd.com
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 2/11
InvokedActivity class. The relevant part of the code is here:
Button invokingButton = (Button)findViewById(R.id.invokebutton);
invokingButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent explicitIntent = new
Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
}
});
The layout for InvokingActivity is defined in /res/main.xml:
and for InvokedActivity in /res/invokedactivity.xml.
Here are our java code, InvokingActivity.java:
package com.bogotobogo.explicitintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class InvokingActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button invokingButton = (Button)findViewById(R.id.invokebutton);
invokingButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent explicitIntent = new
Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
}
});
}
}
and InvokedActivity.java:
package com.bogotobogo.explicitintent;
import android.app.Activity;
import android.os.Bundle;
public class InvokedActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invokedactivity);
}
}
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 3/11


Files used in this Explicit Intent example, ExplicitIntent.zip

In the next section, we will see how to work with implicit intents which also needs us to understand
filters.

13.2 Implicit Intent
In the previous section, we learned how to use Explicit Intents to invoke activities through a very simple
example. Now, we will move on to a more interesting concept of Implicit Intents and
As described earlier, an implicit intent does not name a target component that should act upon the intent.
Android resolves as to which component is best suited to respond to an Implicit Intent
Basically, an Intent object has the following information (among other things like Component name, extras and
flags) which is of interest for implicit intents:
Action
Category
Data
So, Android compares the three (action, category and data) to something called Intent Filters
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 4/11
by probable target components who are willing to accept Implicit Intent calls. i.e. Intent Filters are the
any component to advertise its own capabilities to the Android system. This is done declaratively in the
AndroidManifest.xml file.
So here are some important points to remember:
1. Implicit Intents do not specify a target component.
2. Components willing to receive implicit intents have to declare their ability to handle a specific intent by
declaring intent filters.
3. A component can declare any number of Intent Filters.
4. There can be more than one component that declares the same Intent Filters
the same implicit intent. In that case, the user is presented both the component options and he can
choose which one he wants to continue with.
5. We can set priorities for the intent filters to ensure the order of responses.
There are 3 tests conducted in order to match an intent with intent filters:
1. Action Test
2. Category Test
3. Data Test
Finally, we'll look at declaring an implicit intent in one activity which will invoke one of the native activities of the
platform by matching the intent filters declared by the same.
The ImplicitIntent Activity creates an implicit intent object contacts. This intent object's component is not set.
However, the action is set to android.content.intent.ACTION_VIEW and
People.CONTENT_URI.
Such an intent matches with the intent filter declared by the view contacts native activity.
So, when we run this application, it displays the native UI for viewing the existing contacts on the phone!
Here is the relevant piece of code for the same:
Button viewContacts = (Button)findViewById(R.id.ViewContacts);

viewContacts.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});
In this manner many of the native applications can be seamlessly invoked as one of the activities in our
applications through implicit intents.
Here are our Java code, ImplicitIntent.java:
package com.bogotobogo.implicitintent;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 5/11
public class ImplicitIntent extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ViewContacts();

}

private void ViewContacts() {
try {
Button viewContacts = (Button)findViewById(R.id.ViewContacts);

viewContacts.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
}
});
}catch (ActivityNotFoundException anfe) {
Log.e("ViewContacts","Viewing of Contacts failed", anfe);
}
}
}
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 6/11


Files used in this Implicit Intent example, ImplicitIntent.zip

13.3 Launching a Peer Activity
In this example, we'll have two fields for the latitude and longitude, and a button asking a map for the location.
Here is the 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">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1,2">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="4dip"
android:text="Location:"/>
<EditText android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
android:layout_weight="1"/>
<EditText android:id="@+id/lon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
<Button android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show me the map!"/>
</LinearLayout>
Our Java code:
package com.bogotobogo.Launch;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 7/11
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Launch extends Activity {
private EditText lat;
private EditText lon;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

Button btn=(Button)findViewById(R.id.map);
lat=(EditText)findViewById(R.id.lat);
lon=(EditText)findViewById(R.id.lon);

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String _lat=lat.getText().toString();
String _lon=lon.getText().toString();
Uri uri=Uri.parse("geo:"+_lat+","+_lon);

startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}
}
The button's OnClickListener takes the latitude and longitude, put them into geo scheme Uri.
Uri uri=Uri.parse("geo:"+_lat+","+_lon);
Then, starts the activity after creating an intent requesting to view this Uri (ACTION_VIEW)
startActivity(new Intent(Intent.ACTION_VIEW, uri));
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 8/11


13.4 Intent Tabs
In this section, we'll have a tab browser using an Intent. Each tab will launch its own browser Activity. Actually
Android's tab-management framework the Activity's UI into each tab.
Here is the source for our main activity which is hosting the TabView, IntentTab.java:
package com.bogotobogo.intenttab;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class IntentTab extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost host=getTabHost();

host.addTab(host.newTabSpec("one").setIndicator("BoGoToBoGo").
setContent(new Intent(this, BoGoBrowser.class)));
host.addTab(host.newTabSpec("two").setIndicator("Android").
setContent(new Intent(this, AndroidBrowser.class)));
}
}
Here, we are using TabActivity as the base class, and so we don't have to use our own layout for the view since
TabActivity supplies it for us. So, we just get access to the TabHost and add two tabs. Each tab specifies an
Intent that directly refers to another class: BoGoBrowser and AndroidBrowser, respectively.

Other sources we need are:

BoGoBrowser.java:
package com.bogotobogo.intenttab;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class BoGoBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl("http://bogotobogo.com");
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 9/11
browser.loadUrl("http://bogotobogo.com");
}
}
AdroidBrowser.java:
package com.bogotobogo.intenttab;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class AndroidBrowser extends Activity {
WebView browser;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

browser=new WebView(this);
setContentView(browser);
browser.loadUrl("http://www.android.com/");
}
}
One more thing, we need to add the following lines to AndroidManifest.xml.
<activity android:name=".BoGoBrowser" />
<activity android:name=".AndroidBrowser" />
<uses-permission android:name="android.permission.INTERNET" />
So, the manifest file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bogotobogo.intenttab"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntentTab"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BoGoBrowser" />
<activity android:name=".AndroidBrowser" />
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 10/11
Comments (0)



Source: bogotobogo
Bookmark with:

Only registered users can write comments!
Powered by !JoomlaComment 4.0 beta2

Android Core - Android Tutorial, Powered by Joomla!
28/03/2011 Android Applications Tutorial: Intent
androidcore.com/…/627.html 11/11

. In the file.. EvangelineVincent33 How to implement you. we actually specify the activity that is required to respond to the we explicitly designate the target component. we just declare an inte platform to find an activity that can respond to the intent.intent.. Because this activity is the main one for the application. tel.action. re .LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name= "android. Android will find the best matching activity and pass the intent over to the activity.category.READ_CONTACTS"/> </manifest> Killer Android Tweet App Killer App with new Live Preview That Shows Links In Tweets-Free www. Once we have our intent. This will cause Android to find the best matchin intent to the activity for handling.MAIN" /> <category android:name="android. how to compile a code on android.intent. The 13.28/03/2011 Android Notification SDK Developers Add Notifications In <10 Min.permission. ACTION_EDIT.com An Intent provides a facility for performing late runtime binding between the code in di most significant use is in the launching of activities..xml file. The activity will not be informed when the child activit Call startActivityForResult(). Here.html 2/11 .com/apk/res/android" package="com. So..I understood the code .ch All Android components that wish to be notified via intents should declare intent filters which intents should go to that component.com/…/627.myContacts" android:versionCode="1" android:versionName="1. reply this topic ..bogotobogo. such as ACTION_VIEW. Note that intent-filter element is under the activity element..1 Explicit Intent In an explicit intent.All people deserve wealthy life .myContacts" android:label="@string/app_name"> <intent-filter> <action android:name= "android.buzzbox.Twidroyd. we do not declare the hence is typically used for activating components of other applications seamlessly Let's look at our example: This example has 2 activities: InvokingActivity InvokedActivity The InvokingActivity has a button "Invoke Next Activity" which when InvokedActivity class. It looks something like this: <?xml version="1. we are declar (1) the main activity for this application and (2) the activity is in the LAUNCHER catego icon in the Android menu. This is typically used for application interna In an implicit intent.0" encoding="utf-8"?> <manifest xmlns:android= "http://schemas..... the main power of the android design. ACTION_MA data The data to operate on. such as a person record in the contacts database.android.We encourage you y.com Latest Comments ashikch Bounce Animation some error!! . JOHNSInez Compile Android kern. Generator NARM/Network Application Rendering Model www.. we need to add intent-fil AndroidManifest.. Fast. Aneev How to Install the A. we need to pass it to Android and get the child activity to laun options: Call startActivity() with the Intent. Flexible & Free SDK hub. An component it should launch when someone chooses the application from the main menu.. passing it the intent and a number which is unique when the child activity is complete via onActivityResult() callback.bitech-factory..0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=". Aneev 10 Open Source Andro. The relevant part of the code is here: clicke androidcore.. expressed as a Web/XML Appl. where it can be thought of as the glu is basically a passive data structure holding an abstract description of an action to be p pieces of information in an intent are: action Android Applications Tutorial: Intent The general action to be performed.

explicitintent.onCreate(savedInstanceState). The layout for InvokingActivity is defined in /res/main. setContentView(R.view.Bundle.com/…/627.invokebutton).invokedactivity).id.OnClickListener. android. The relevant part of the code is here: Button invokingButton = (Button)findViewById(R.onCreate(savedInstanceState).Button. android. public class InvokedActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.content.invokebutton). public class InvokingActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super. import android.Activity. } } and InvokedActivity.explicitintent. } }).java: package com. android.os.class).java: package com.html 3/11 .xml. startActivity(explicitIntent).View.app.view. startActivity(explicitIntent). } }).layout. Here are our java code. import import import import import import android.28/03/2011 Android Applications Tutorial: Intent InvokedActivity class. Button invokingButton = (Button)findViewById(R. setContentView(R.os.Bundle.Intent.widget.class).bogotobogo.InvokedActivity. android. invokingButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent explicitIntent = new Intent(InvokingActivity. import android. } } androidcore. InvokingActivity.this.View.main).xml: and for InvokedActivity in /res/invokedactivity.app.layout. invokingButton.Activity.InvokedActivity.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent explicitIntent = new Intent(InvokingActivity. android.bogotobogo.id.this.

an Intent object has the following information (among other things like Compo flags) which is of interest for implicit intents: Action Category Data So.html 4/11 . Now.com/…/627.zip In the next section. we will move on to a more interesting concept of Implicit Intents and As described earlier.2 Implicit Intent In the previous section.28/03/2011 Android Applications Tutorial: Intent Files used in this Explicit Intent example. we learned how to use Explicit Intents to invoke activities example. ExplicitIntent. an implicit intent does not name a target component that shou Android resolves as to which component is best suited to respond to an Implicit Intent Basically. category and data) to something called Intent F androidcore. 13. Android compares the three (action. we will see how to work with implicit intents which also needs us filters.

we'll look at declaring an implicit intent in one activity which will invoke one of the platform by matching the intent filters declared by the same. There can be more than one component that declares the same Intent Filters choose which one he wants to continue with.ViewContacts). when we run this application. So. android.bogotobogo.ACTION_VI contacts. A component can declare any number of Intent Filters.OnClickListener. 5. viewContacts.content. This is don 2. Inten AndroidManifest.app.content. So here are some important points to remember: 1. android.xml file. There are 3 tests conducted in order to match an intent with intent filters: 1. import import import import import import import import import android. We can set priorities for the intent filters to ensure the order of responses.View.util.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent contacts = new Intent().os.28/03/2011 Android Applications Tutorial: Intent by probable target components who are willing to accept Implicit Intent calls. startActivity(contacts).Intent.ActivityNotFoundException. androidcore. } }). any component to advertise its own capabilities to the Android system. This intent object' However.setData(People.view. In that case. Category Test 3.provider.Intent. Here are our Java code.com/…/627.Contacts. 3.content. android. The ImplicitIntent Activity creates an implicit intent object contacts.html 5/11 .implicitintent.widget.id. contacts.content.view. android. android.ACTION_VIEW and People. the action is set to android. Data Test the same implicit intent.View.Bundle. android.e.Log.People.CONTENT_URI). In this manner many of the native applications can be seamlessly invoked as one o applications through implicit intents.intent. Action Test 2. it displays the native UI for viewing the existing contact Here is the relevant piece of code for the same: Button viewContacts = (Button)findViewById(R.setAction(android. android. Implicit Intents do not specify a target component. Components willing to receive implicit intents have to declare their ability to han declaring intent filters.CONTENT_URI.java: package com. 4.Activity. android.Button. ImplicitIntent. the user is presented both the compone Finally. i. Such an intent matches with the intent filter declared by the view contacts native activity.

"Viewing of Contacts failed".e("ViewContacts". } private void ViewContacts() { try { Button viewContacts = (Button)findViewById(R.CONTENT_URI). } }).main). contacts.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent contacts = new Intent().setData(People. setContentView(R.content. startActivity(contacts). } } } androidcore. anfe). viewContacts. }catch (ActivityNotFoundException anfe) { Log.com/…/627. ViewContacts().html 6/11 .ViewContacts).ACTION_VI contacts.layout.onCreate(savedInstanceState).28/03/2011 Android Applications Tutorial: Intent public class ImplicitIntent extends Activity { /** Called when the activity is first created.Intent.setAction(android.id. */ @Override public void onCreate(Bundle savedInstanceState) { super.

and a button asking a Our Java code: package com.app.html 7/11 .0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas. androidcore. we'll have two fields for the latitude and longitude.Intent. import android.content. import android. ImplicitIntent.Launch.3 Launching a Peer Activity Here is the layout: <?xml version="1.zip 13.28/03/2011 Android Applications Tutorial: Intent Files used in this Implicit Intent example. import android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1.bogotobogo.com/…/627.net.Uri.2"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="2dip" android:paddingRight="4dip" android:text="Location:"/> <EditText android:id="@+id/lat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cursorVisible="true" android:editable="true" android:singleLine="true" android:layout_weight="1"/> <EditText android:id="@+id/lon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cursorVisible="true" android:editable="true" android:singleLine="true" android:layout_weight="1"/> </TableRow> </TableLayout> <Button android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show me the map!"/> </LinearLayout> In this example.Activity.android.

id."+_lon).parse("geo:"+_lat+". androidcore. setContentView(R.View. } } The button's OnClickListener takes the latitude and longitude.html 8/11 .setOnClickListener(new View.ACTION_VIEW } }).widget. @Override public void onCreate(Bundle icicle) { super.parse("geo:"+_lat+".main).ACTION_VIEW. lon=(EditText)findViewById(R.lon). android.onCreate(icicle). String _lon=lon.lat).getText().OnClickListener() { public void onClick(View view) { String _lat=lat. put them into geo scheme Uri uri=Uri.id.28/03/2011 Android Applications Tutorial: Intent import import import import android.Bundle. private EditText lon. public class Launch extends Activity { private EditText lat.os.layout. Uri uri=Uri.Button. Then."+_lon).map). uri)).toString(). startActivity(new Intent(Intent.widget.view.getText(). btn. android.EditText. Button btn=(Button)findViewById(R. starts the activity after creating an intent requesting to view this Uri (ACTION_VIEW startActivity(new Intent(Intent. lat=(EditText)findViewById(R. android.id.com/…/627.toString().

we are using TabActivity as the base class.html 9/11 . TabHost host=getTabHost().addTab(host. import android. import android. So.intenttab.setIndicator("Android").onCreate(savedInstanceState).app.newTabSpec("one"). we'll have a tab browser using an Intent. host. browser=new WebView(this). public class BoGoBrowser extends Activity { WebView browser.webkit.widget.Bundle. we just get access to the TabHost and add two tabs Intent that directly refers to another class: BoGoBrowser and AndroidBrowser.intenttab.onCreate(icicle). import import import import android.bogotobogo. respectively Other sources we need are: BoGoBrowser.4 Intent Tabs Android's tab-management framework the Activity's UI into each tab.addTab(host.java: package com.28/03/2011 Android Applications Tutorial: Intent 13.class))).os. BoGoBrowser. setContent(new Intent(this.Intent. @Override public void onCreate(Bundle icicle) { super.TabHost. and so we don't have to use our own la TabActivity supplies it for us.java: package com.content.bogotobogo.WebView. In this section. android.loadUrl("http://bogotobogo. browser. android. androidcore.Activity.Bundle. android.setIndicator("BoGoToBoGo setContent(new Intent(this. Here is the source for our main activity which is hosting the TabView.com").com/…/627. AndroidBrowser. Each tab will launch its own br public class IntentTab extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super. import android.newTabSpec("two"). setContentView(browser).TabActivity.os.app.class))) } } Here. IntentTab. host.

Activity.bogotobogo.loadUrl("http://www.android.AndroidBrowser" /> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.loadUrl("http://bogotobogo.BoGoBrowser" /> <activity android:name=".INTERNET" /> So.intenttab" android:versionCode="1" android:versionName="1. <activity android:name=".bogotobogo.html 10/11 .app.IntentTab" android:label="@string/app_name"> <intent-filter> <action android:name="android. import android. setContentView(browser). import android.INTERNET" /> </manifest> androidcore.permission. public class AndroidBrowser extends Activity { WebView browser. browser=new WebView(this).com/…/627. import android.com/apk/res/android" package="com.BoGoBrowser" /> <activity android:name=".os.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.intent.0"> <application android:icon="@drawable/icon" android:label="@string/app_n <activity android:name=". @Override public void onCreate(Bundle icicle) { super.onCreate(icicle).category. } } AdroidBrowser.intenttab.xml.28/03/2011 Android Applications Tutorial: Intent browser.WebView. browser.intent.webkit.com/").AndroidBrowser" /> <uses-permission android:name="android. } } One more thing.MAIN" /> <category android:name="android.com").android. we need to add the following lines to AndroidManifest. the manifest file should look like this: <?xml version="1.action.LAUNCHER" / </intent-filter> </activity> <activity android:name=".Bundle.java: package com.permission.

28/03/2011 Android Applications Tutorial: Intent Source: bogotobogo Bookmark with: Comments (0) Only registered users can write comments! Powered by !JoomlaComment 4.Android Tutorial. Powered by Joomla! androidcore.html 11/11 .com/…/627.0 beta2 Android Core .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times