Call a number directly from an Android application

June 25, 2012
By

In this article we will create an application which can call a telephone number directly from the application. In order to call a number we will make use the standard action called ACTION_CALL. An application can  call a number direclty if  and only if  the necessary permission is explicitly granted. For this we need to provide CALL_PHONE permission to the application.


1. Start a new Android project namely “CallDemo”

New Android Project

Figure 1: New Android Project


2. Select build target for the application

Select Build Target

Figure 2: Select Build Target

  


3. Enter application details

Enter Application Details

Figure 3: Enter Application Details


4. res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">DialDemo</string>
    <string name="btn">Call</string>
    <string name="telNoHintText">Enter a telephone number ...</string>
</resources>


5. res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/telNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/telNoHintText"
        android:inputType="number"
    />

    <Button
        android:id="@+id/btnCall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/btn"

    />

</LinearLayout>


6. AndroidManifest.xml

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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-permission android:name="android.permission.CALL_PHONE" />
</manifest>

7. src/in/wptrafficanalyzer/calldemo/MainActivity.java


package in.wptrafficanalyzer.dialdemo;

import android.app.Activity;
import android.content.Intent;
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;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Defining an onclick listener */
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                /** Get Telephone number Object*/
                EditText telNo = (EditText) findViewById(R.id.telNo);

                /** Get Telephone number String **/
                String strTelNo = telNo.getText().toString();

                /** Creating an intent which invokes an activity whose action name is ACTION_CALL */
                Intent intent = new Intent("android.intent.action.CALL");

                /** Creating a uri object to store the telephone number */
                Uri data = Uri.parse("tel:"+ strTelNo );

                /** Setting intent data */
                intent.setData(data);

                /** Starting the caller activity by the implicit intent */
                startActivity(intent);

            }
        };

        /** Getting reference to button of main.xml */
        Button btn = (Button) findViewById(R.id.btnCall);

        /** Setting the event listener for the button */
        btn.setOnClickListener(listener);

    }
}


8. Application in execution

Dial Demo Main Activity

Figure 4 : Dial Main Activity

Call Activity

Figure 5: Call Activity


9. Download


10. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

One Response to Call a number directly from an Android application

  1. dina salah on August 26, 2014 at 1:40 pm

    Thanks for this demo,
    its really helped me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner