Calling Built in Apps
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Calling Built in Apps

Until this point, you have seen how to call activities within your own application. One of the key aspects 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 Android device. For example, if your application needs to enable a user to call a particular person saved in the 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 consistent user experience, and enables you to avoid building another application to retrieve all the contacts in the Contacts application.

  • Using Eclipse, create a new Android project and name it Intents.
  • 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>

 

  • 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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---Web browser button---
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);
}
}
}
}

 

  • Press F11 to debug the application on the Android Emulator.
  • Click the Web Browser button to load the Browser application on the emulator

Calling Built-in-app
 

  • Click the Make Calls button and the Phone application will load.
  • Similarly, to load the Maps application, shown in Figure, click the Show Map button.
     
  • Click the Choose Contact application to show a list of contacts that you can select (see Figure ). Selecting a contact will show details about that contact.

Calling Built in app
 

Calling Built in app
 

Calling Built in app

 

 

← Preview

Communication Between Activities

Next →

Dealing With Intent Object And Intent Filters

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App