Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Phone call


We are able to make a phone call in android via intent. You need to write only three lines of code to make a phone call.

  1. Intent callIntent = new Intent(Intent.ACTION_CALL);
  2. callIntent.setData(Uri.parse(“tel:”+8802177690));//change the number
  3. startActivity(callIntent);

Example of phone call in android

activity_main.xml

Drag the EditText and Button from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <Button
  7.         android:id=“@+id/button1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“118dp”
  13.         android:text=“Call” />
  14.     <EditText
  15.         android:id=“@+id/editText1”
  16.         android:layout_width=“wrap_content”
  17.         android:layout_height=“wrap_content”
  18.         android:layout_alignParentTop=“true”
  19.         android:layout_centerHorizontal=“true”
  20.         android:layout_marginTop=“25dp”
  21.         android:ems=“10” />
  22. </RelativeLayout>

Write the permission code in Android-Manifest.xml file

You need to write CALL_PHONE permission as given below:

  1. <uses-permission android:name=“android.permission.CALL_PHONE” />
File: Android-Manifest.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <manifest xmlns:androclass=“http://schemas.android.com/apk/res/android”
  3.     package=“com.example.phonecall”
  4.     android:versionCode=“1”
  5.     android:versionName=“1.0” >
  6.     <uses-sdk
  7.         android:minSdkVersion=“8”
  8.         android:targetSdkVersion=“16” />
  9.     <uses-permission android:name=“android.permission.CALL_PHONE” />
  10.     <application
  11.         android:allowBackup=“true”
  12.         android:icon=“@drawable/ic_launcher”
  13.         android:label=“@string/app_name”
  14.         android:theme=“@style/AppTheme” >
  15.         <activity
  16.             android:name=“com.example.phonecall.MainActivity”
  17.             android:label=“@string/app_name” >
  18.             <intent-filter>
  19.                 <action android:name=“android.intent.action.MAIN” />
  20.                 <category android:name=“android.intent.category.LAUNCHER” />
  21.             </intent-filter>
  22.         </activity>
  23.     </application>
  24. </manifest>

Activity class

Let’s write the code to make the phone call via intent.

File: MainActivity.java
  1. package com.example.phonecall;
  2. import android.net.Uri;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. public class MainActivity extends Activity {
  12.     EditText edittext1;
  13.     Button button1;
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         //Getting the edittext and button instance
  19.         edittext1=(EditText)findViewById(R.id.editText1);
  20.         button1=(Button)findViewById(R.id.button1);
  21.         //Performing action on button click
  22.         button1.setOnClickListener(new OnClickListener(){
  23.             @Override
  24.             public void onClick(View arg0) {
  25.                 String number=edittext1.getText().toString();
  26.                 Intent callIntent = new Intent(Intent.ACTION_CALL);
  27.                 callIntent.setData(Uri.parse(“tel:”+number));
  28.                 startActivity(callIntent);
  29.             }
  30.         });
  31.     }
  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         // Inflate the menu; this adds items to the action bar if it is present.
  35.         getMenuInflater().inflate(R.menu.activity_main, menu);
  36.         return true;
  37.     }
  38. }

download this example

Install and Run the apk file on the Real Device (e.g. Mobile) to make the phone call.


Output:

phoneoutput1

phoneoutput2

Comments

comments