Make a call by using Intent.ACTION_CALL in Android

In this article, we are going to use Intent.ACTION_CALL for making a call from our Android device on a specific telephone number.
Submitted by Manu Jemini, on February 17, 2018

In the example below, we are going to use the Intent and Bundle class to making a phone call programmatically from your app with intent.

To implement this, in your program you will need to import: android.content.Intent, android.content.pm.PackageManager in your java file.

Then create two layout files first with a single TextView and Button. Button should be making like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click here to call"
	android:id="@+id/button"
	android:onClick="call"
	android:layout_alignParentBottom="true"
	android:layout_centerHorizontal="true"
	android:layout_marginBottom="161dp" />

In the Button above, we have put an onClick property and set it to call function which is in the java File. We assign on click function directly like this : android:onClick="call".

First we will make an Intent object of type intent.ACTION_CALL. Next, we set data to that object with the URI parse method.

    Intent call = new Intent(Intent.ACTION_CALL);
    call.setData(Uri.parse("tel:1234567890"));

At last we take the permission and start call.

1) Java file:

package com.example.hp.myapplication;

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

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void call(View arg0){
        Intent call = new Intent(Intent.ACTION_CALL);
        call.setData(Uri.parse("tel:1234567890"));
        if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
            return;
        }
        startActivity(call);
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to call"
        android:id="@+id/button"
        android:onClick="call"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="161dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Phone number: 1234567890"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="101dp"
        android:layout_above="@+id/button"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

call by using Intent.ACTION_CALL in Android 1
call by using Intent.ACTION_CALL in Android 2


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.