Friday 8 May 2015

Calling Built-In Application using Intent in android

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.
The following Example demonstrates how to call some of the built-in applications commonly found on an Android device.

Calling Built-in Applications Using intents


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 com.emergingandroidtech.Intents;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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://emergingandroidtech.blogspot.in/"));
                */
               
                //---OR---
                Intent i = new
                Intent("android.intent.action.VIEW");               
                //Intent(android.content.Intent.ACTION_VIEW);               
                i.setData(Uri.parse("http://emergingandroidtech.blogspot.in/"));
               
                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);
                startActivity(i);
                */
               
                //---OR---
               
                Intent i = new
                    Intent(android.content.Intent.ACTION_CALL,
                  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);
                i.setType(ContactsContract.CommonDataKinds.Phone.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 .Click the Web Browser button to load the Browser application on the emulator.
 

5 . Click the Make Calls button and the Phone application will load .

6 . Similarly, to load the Maps application, click the Show Map button.
 

NOTE  In order to display the Maps application, you need to run the application on an AVD that supports the Google APIs.
 

7 . Click the Choose Contact application to show a list of contacts that you can select . Selecting a contact will show details about that contact.

No comments:

Post a Comment