Android

Android Phone Calls

Android Phone Calls

In Android, you will get built-in applications for making calls. You can also add this functionality to your application with the help of PhoneStateListener and TelephonyManager classes. 

The steps needed to add the SMS functionality to your application are described below:

1. First, you need to have an Android Studio IDE to create an application. Let’s create an application with the name Phone under our package com.firstapp.greatlearning.

2. Now you need to modify the MainActivity.java file that can be found under your src folder. You can use the code snippet shown below to implement this:

package com.firstapp.greatlearning;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   private Button btn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btn = (btn) findViewById(R.id.buttonCall);
      
      btn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
            Intent phoneCall = new Intent(Intent.ACTION_CALL);
            phoneCall.setData(Uri.parse("Mob: 9999999999"));
            
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
               Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                  return;
               }
               startActivity(phoneCall);
         }
      });

   }
}

3. After that, you will have to modify the layout XML file with the name activity_main.xml to add any GUI component to your application. The following code and configurations can be used for your reference:

<?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" >

   <Button
      android:id="@+id/buttonCall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Call 9999999999" />

</LinearLayout>

4. Next, we are going to modify the AndroidManifest.xml file as shown below and run the application:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.firstapp.greatleraning" >
   
   <uses-permission android:name="android.permission.CALL_PHONE" />
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/CustomTheme" >
      
      <activity
         android:name="com.example.saira_000.myapplication.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>
</manifest>

This way you can add the call functionality to your application. When you run the application, it will show you the text ‘Call 9999999999’ and when you click on that, it will start calling this number with another interface of a phone call. Try this yourself on your Android Emulator which will help you to better understand the concept. 
 

Top course recommendations for you

    My SQL Basics
    5 hrs
    Beginner
    264.7K+ Learners
    4.46  (14007)
    Android Application Development
    2 hrs
    Beginner
    161.3K+ Learners
    4.42  (7050)
    OOPs in Java
    2 hrs
    Beginner
    115.4K+ Learners
    4.45  (6770)
    Building Games using JavaScript
    2 hrs
    Beginner
    33.5K+ Learners
    4.46  (655)
    Introduction to DevOps
    3 hrs
    Beginner
    68.9K+ Learners
    4.56  (4346)
    Introduction To AngularJS
    2 hrs
    Beginner
    25.5K+ Learners
    4.54  (1090)
    Introduction to JavaScript
    3 hrs
    Beginner
    104.9K+ Learners
    4.46  (6073)
    Data Structure & Algorithms in Java for Intermediate Level
    4 hrs
    Intermediate
    16K+ Learners
    4.48  (3122)
    Building Games using Java
    2 hrs
    Beginner
    28.6K+ Learners
    4.37  (234)
    Algorithms in C
    3 hrs
    Beginner
    30.1K+ Learners
    4.43  (999)