"Until this point, you have seen how to call activities within your own application. One of the keyaspects of Android programming is using the intent to call activities from other applications. In particular,your application can call the many built-in applications that are included with an Androiddevice. For example, if your application needs to enable a user to call a particular person saved inthe Contacts application, you can simply use an Intent object to bring up the Contacts application,from which the user can select the person to call. This enables your application to present a consistentuser experience, and enables you to avoid building another application to retrieve all the contacts in the Contacts application."
The following demonstrates how to call some of the built-in applications commonly
found on an Android device.
STEPS:
1.Using Eclipse, create a new Android project and name it Intents.
2. Add the following statements in bold to the main.xml file:
<?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/btn_webbrowser”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Web Browser” />
<Button
android:id=”@+id/btn_makecalls”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Make Calls” />
<Button
android:id=”@+id/btn_showMap”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Show Map” />
<Button
android:id=”@+id/btn_chooseContact”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Choose Contact” />
</LinearLayout>
3.Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button b1, b2, b3, b4;
int request_Code = 1;
// Called when the activity is first created.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.btn_webbrowser);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“http://www.amazon.com”));
startActivity(i);
}
});
//Make calls button---
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_DIAL,
Uri.parse(“tel:+651234567”));
startActivity(i);
}
});
//Show Map button
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“geo:37.827500,-122.481670”));
startActivity(i);
}
});
//Choose Contact button
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0){
Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(i,request_Code);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code)
{
if (resultCode == RESULT_OK)
{
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
Intent i = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse(data.getData().toString()));
startActivity(i);
}
}
}
}
4 Press F11 to debug the application on the Android Emulator.
5 Click the Web Browser button to load the Browser application on the emulator as shown in
Figure.
Fig - Loading Browser Application in Emulator