Saturday, June 22, 2013

Dial Number In Android Phone

Today I am sharing the code for dial a number to call any guys from android device. 
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 directly if  and only if  the necessary permission is explicitly granted. For this we need to provide CALL_PHONE permission to the application.


Main.xml


1
2
3
4
5
6
7
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <edittext android:hint="@string/telNoHintText" android:id="@+id/telNo" android:inputtype="number" android:layout_height="wrap_content" android:layout_width="fill_parent">
     
    <button android:id="@+id/btnCall" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/btn">
</button></edittext></linearlayout>

MainActivity.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.sunil.call;
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 implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button btn = (Button) findViewById(R.id.btnCall);       
        btn.setOnClickListener(this);
         
    }
       
   @Override
   public void onClick(View v) {
   
    EditText telNo = (EditText) findViewById(R.id.telNo);
    String strTelNo = telNo.getText().toString();
    Intent intent = new Intent("android.intent.action.CALL");
    
    Uri data = Uri.parse("tel:"+ strTelNo );
    intent.setData(data);
    startActivity(intent);
         
    }
}

Menifest.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<manifest android:versioncode="1" android:versionname="1.0" package="com.sunil.call" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minsdkversion="10">   
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name="com.sunil.call.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN">
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
             
        </activity>
    </application>
    <uses-permission android:name="android.permission.CALL_PHONE">
</uses-permission></uses-sdk></manifest>


No comments:

Post a Comment