Question Detail

Hello Developers,
I have a button which is for Call directly to the set Phone Number I want to call directly to the Customer Care not to open number and then touch on Call button.

How to make a phone call Programmatically in android? 


Thread Reply

Hemant Sharma

- 8 days ago

You have call CALL_PHONE  permission to perform this task.

Add Permission in Manifest.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

 

Now your Business Logic in MainActivity.java

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Activity mActivity;
    final int CALLPHONEREQUESTCODE = 89;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        mActivity = MainActivity.this;
        callDirectlyMethod();
    }

    public void callDirectlyMethod() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CALL_PHONE}, CALLPHONEREQUESTCODE);
        } else {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:+91 9000000000"));
            startActivity(callIntent);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CALLPHONEREQUESTCODE:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    callDirectlyMethod();
                } else {
                    Toast.makeText(mActivity, "Request Rejected.", Toast.LENGTH_SHORT).show();
                }
                break;

            default:
                break;
        }
    }
}

Hemant Sharma

- 8 days ago

You have call CALL_PHONE  permission to perform this task.

Add Permission in Manifest.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

 

Now your Business Logic in MainActivity.java

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Activity mActivity;
    final int CALLPHONEREQUESTCODE = 89;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        mActivity = MainActivity.this;
        callDirectlyMethod();
    }

    public void callDirectlyMethod() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CALL_PHONE}, CALLPHONEREQUESTCODE);
        } else {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:+91 9000000000"));
            startActivity(callIntent);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CALLPHONEREQUESTCODE:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    callDirectlyMethod();
                } else {
                    Toast.makeText(mActivity, "Request Rejected.", Toast.LENGTH_SHORT).show();
                }
                break;

            default:
                break;
        }
    }
}