Tuesday, 29 October 2013

Call using Intent

Basics about Intents:

Intent an "intention" to do an action. Intents are asynchronous messages which allow application components to request functionality from other Android components. They allow you to interact with components from own and other applications. For example an activity can start an external activity for taking a picture.See here.
In this post I'll tell you, about how to use the built-in Call app to call using Intent.ACTION_CALL by passing cell number as data. This post requires EditText to get number from user and a button to call Intent.

OutPut:

Make Call using intent

On Entering Number

Make Call using intent

On Call

Make Call using intent

Create new Android Project
Project Name: MakeCall
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Make Call
Package Name: com.shaikhhamadali.blogspot.makecall
Create layout file: activity_make_call

  1. create layout:



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MakeCall" >

    <EditText
        android:id="@+id/eTNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:ems="10"
        android:hint="Enter number to call"
        android:inputType="phone" />

    <Button
        android:id="@+id/btnCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eTNumber"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:text="Call"
        />

</RelativeLayout>

-Manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Shaikhhamadali.blogspot.makecall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.Shaikhhamadali.blogspot.makecall.MakeCall"
            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>

</manifest>

2.code of activity:


package com.Shaikhhamadali.blogspot.makecall;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class MakeCall extends Activity {
 //variables
 EditText eTNumber;
 Button btnCall;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_make_call);
  //create instance of Edit Text to get number to call 
  eTNumber=(EditText)findViewById(R.id.eTNumber);
  //create instance of Button to make Call
  btnCall=(Button)findViewById(R.id.btnCall);
  btnCall.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //call method we initialized for making a call
    makeCall(); 
   }
  });
 }

 protected void makeCall() {
  //create instance of Intent and pass Intent.ACTION_CALL
  /*Intent.ACTION_CALL is Action to Perform a call to someone specified by the data. 
    If input is nothing, an empty dialer is started; else getData() is URI of a phone number
    to be dialed or a tel: URI of an explicit phone number.
   */
  Intent phoneIntent = new Intent(Intent.ACTION_CALL);
  //set data with tel: +(number to call)
  phoneIntent.setData(Uri.parse("tel:"+eTNumber.getText().toString()));
  try {
   //start activity to perform call action
   startActivity(phoneIntent);

  }
  //catch if exception generated and show toast
  catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(MakeCall.this, 
     "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
  }
 }

}

3. note that:

  • I have used this to call on a number user entered,but you can use it the way you want.
  • ACTION_CALL this action used to trigger built-in phone call functionality available in Android device. 
  • Learn more about ACTION_CALL.

4. conclusion:

  • Some information about how to call using built-in phone call functionality available in Android device.
  • know how to use Action to built-in functionality.

5. About the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:

        You can download the source code from: GoogleDrive,GitHub