How to make a phone call using intent in Android?
Asked Answered
S

21

385

I'm using the following code to make a call in Android but it is giving me security exception please help.

 posted_by = "111-333-222-4";

 String uri = "tel:" + posted_by.trim() ;
 Intent intent = new Intent(Intent.ACTION_CALL);
 intent.setData(Uri.parse(uri));
 startActivity(intent);

permissions

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

Exception

11-25 14:47:01.661: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
11-25 14:47:01.681: ERROR/AndroidRuntime(302): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:111-333-222-4 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43d32508 302:com.Finditnear/10026} (pid=302, uid=10026) requires android.permission.CALL_PHONE
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.os.Parcel.readException(Parcel.java:1218)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.os.Parcel.readException(Parcel.java:1206)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1214)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Activity.startActivityForResult(Activity.java:2749)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.app.Activity.startActivity(Activity.java:2855)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at com.Finditnear.PostDetail$2$1$1$1.onClick(PostDetail.java:604)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.ListView.performItemClick(ListView.java:3285)
11-25 14:47:01.681: ERROR/AndroidRuntime(302):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
Succor answered 25/11, 2010 at 9:49 Comment(0)
S
79

Every thing is fine.

i just placed call permissions tag before application tag in manifest file

and now every thing is working fine.

Succor answered 25/11, 2010 at 10:20 Comment(4)
See my answer below on how to achieve almost the same without the need for a permission and with a chance for the user to not actually make the call.Flann
Also, be aware of telephony-less devices : https://mcmap.net/q/88081/-optional-permissions-so-an-app-can-show-on-all-devices-and-enable-optional-features-on-someConvolvulus
further info as suggested by Snicolas londatiga.net/it/programming/android/…Compunction
You will not be able to submit your app to the play store this way. Use Action Dial instead as your Intent and remove Call Phone from the manifest.Townley
F
420

You can use Intent.ACTION_DIAL instead of Intent.ACTION_CALL. This shows the dialer with the number already entered, but allows the user to decide whether to actually make the call or not. ACTION_DIAL does not require the CALL_PHONE permission.

Flann answered 29/10, 2012 at 14:30 Comment(1)
ACTION_DIAL not require permission, this is an important difference compared with ACTION_CALL intent :)Homework
M
268

This demo will helpful for you...

On call button click:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);

Permission in Manifest:

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

EDIT IN 2021

You should write it in your manifest file but at the same time you should ask in runtime.Like this code:

if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CALL_PHONE),
                REQUEST_CODE)

        } else {

// else block means user has already accepted.And make your phone call here.

}

And if you want you can override onRequestPermissionsResult to give user better experience if you write same code with else block here your user will not need to click on your button again after you give permission it will directly call.

Metalwork answered 24/9, 2013 at 5:19 Comment(5)
+1 for "tel:" . I had call instead and got no intent exception. TnxSubhead
This simply does not work in my nexus 4. It opens the dialer showing the phone number but does not start the call without user interaction. Any sugestion?Moser
What's the difference between the code in the question and in this answer? How does it solve the problem?Brotherly
This opens skype for me.Sands
Please add Call Phone Run time Permission tooIntersperse
C
230

More elegant option:

String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);
Chatav answered 23/6, 2014 at 13:37 Comment(3)
This works without adding anything to Manifest like the previous answerDolley
No runtime permissions required. +1Oodles
if you are running this code outside the Activity class then you need to add Intent.FLAG_ACTIVITY_NEW_TASK to your intent.Swansea
C
109

Use the action ACTION_DIAL in your intent, this way you won't need any permission. The reason you need the permission with ACTION_CALL is to make a phone call without any action from the user.

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0987654321"));
startActivity(intent);
Chao answered 13/1, 2015 at 21:29 Comment(2)
This is better in case where you don't need to ask permission.Postaxial
There's a missing semicolon in the second line of the code. perfect answer!Cowgirl
S
79

Every thing is fine.

i just placed call permissions tag before application tag in manifest file

and now every thing is working fine.

Succor answered 25/11, 2010 at 10:20 Comment(4)
See my answer below on how to achieve almost the same without the need for a permission and with a chance for the user to not actually make the call.Flann
Also, be aware of telephony-less devices : https://mcmap.net/q/88081/-optional-permissions-so-an-app-can-show-on-all-devices-and-enable-optional-features-on-someConvolvulus
further info as suggested by Snicolas londatiga.net/it/programming/android/…Compunction
You will not be able to submit your app to the play store this way. Use Action Dial instead as your Intent and remove Call Phone from the manifest.Townley
L
38

IMPORTANT NOTE:

If you use Intent.ACTION_CALL you must add CALL_PHONE permission.

Its okey only if you don't want your app to show up in google play for tablets that doesn't take SIM card or doesn't have GSM.

IN YOUR ACTIVITY:

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
            startActivity(callIntent);

MANIFEST:

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

So if it is not critical feature to your app, try to stay away from adding CALL_PHONE permission.

OTHER SOLUTION:

Is to show the Phone app with the number written in on the screen, so user will only need to click call button:

            Intent dialIntent = new Intent(Intent.ACTION_DIAL);
            dialIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
            startActivity(dialIntent);

No permission needed for this.

Lines answered 21/8, 2017 at 6:30 Comment(0)
I
25

Just the simple oneliner without any additional permissions needed:

private void dialContactPhone(final String phoneNumber) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
Indihar answered 14/8, 2015 at 12:29 Comment(0)
H
19

use this full code

          Intent callIntent = new Intent(Intent.ACTION_DIAL);
          callIntent.setData(Uri.parse("tel:"+Uri.encode(PhoneNum.trim())));
          callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(callIntent);     
Heraclitean answered 27/1, 2014 at 9:22 Comment(1)
This technique worked for me, although I had to change Intent.ACTION_DIAL to Intent.Anction_CALL.Balsamiferous
D
13

Request Permission in manifest

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

For calling use this code

Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:99xxxxxxxx"));
try {
    startActivity(in);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(mcontext, "Could not find an activity to place the call.", Toast.LENGTH_SHORT).show();
}
Diahann answered 20/11, 2015 at 7:51 Comment(1)
I think you don't need the permission since your app is not calling itself, but asking the dedicated app (which has the permission) to do it.Briscoe
G
12
// Java
String mobileNumber = "99XXXXXXXX";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL); // Action for what intent called for
intent.setData(Uri.parse("tel: " + mobileNumber)); // Data with intent respective action on intent
startActivity(intent);

// Kotlin
val mobileNumber = "99XXXXXXXX"
val intent = Intent()
intent.action = Intent.ACTION_DIAL // Action for what intent called for
intent.data = Uri.parse("tel: $mobileNumber") // Data with intent respective action on intent
startActivity(intent)
Greenes answered 4/10, 2019 at 4:33 Comment(0)
I
11

Permission in AndroidManifest.xml

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

Complete code:

private void onCallBtnClick(){
    if (Build.VERSION.SDK_INT < 23) {
        phoneCall();
    }else {

        if (ActivityCompat.checkSelfPermission(mActivity,
                Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

            phoneCall();
        }else {
            final String[] PERMISSIONS_STORAGE = {Manifest.permission.CALL_PHONE};
            //Asking request Permissions
            ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, 9);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    boolean permissionGranted = false;
    switch(requestCode){
        case 9:
            permissionGranted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
            break;
    }
    if(permissionGranted){
        phoneCall();
    }else {
        Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
    }
}

private void phoneCall(){
    if (ActivityCompat.checkSelfPermission(mActivity,
            Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:12345678900"));
        mActivity.startActivity(callIntent);
    }else{
        Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
    }
}
Impeach answered 26/10, 2017 at 2:56 Comment(0)
D
7

For call from dialer (No permission needed):

   fun callFromDailer(mContext: Context, number: String) {
        try {
            val callIntent = Intent(Intent.ACTION_DIAL)
            callIntent.data = Uri.parse("tel:$number")
            mContext.startActivity(callIntent)
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
        }
    }

For direct call from app(Permission needed):

  fun callDirect(mContext: Context, number: String) {
        try {
            val callIntent = Intent(Intent.ACTION_CALL)
            callIntent.data = Uri.parse("tel:$number")
            mContext.startActivity(callIntent)
        } catch (e: SecurityException) {
            Toast.makeText(mContext, "Need call permission", Toast.LENGTH_LONG).show()
        } catch (e: Exception) {
            e.printStackTrace()
            Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
        }
    }

Permission:

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Daddylonglegs answered 24/7, 2019 at 7:56 Comment(0)
A
6

You can use this as well:

String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");
Austerlitz answered 4/7, 2013 at 11:4 Comment(0)
S
5

For making a call activity using intents, you should request the proper permissions.

For that you include uses permissions in AndroidManifest.xml file.

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

Then include the following code in your activity

if (ActivityCompat.checkSelfPermission(mActivity,
        Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
    //Creating intents for making a call
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:123456789"));
    mActivity.startActivity(callIntent);

}else{
    Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
}
Secondbest answered 30/6, 2018 at 9:4 Comment(0)
P
3

To avoid this - one can use the GUI for entering permissions. Eclipse take care of where to insert the permission tag and more often then not is correct

Peccavi answered 26/2, 2011 at 21:52 Comment(0)
M
2
 if(ContextCompat.checkSelfPermission(
        mContext,android.Manifest.permission.CALL_PHONE) != 
              PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions((Activity) mContext, new 
                  String[]{android.Manifest.permission.CALL_PHONE}, 0);
                } else {
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Number")));
                }
Megagamete answered 3/4, 2018 at 5:42 Comment(0)
R
1

In Android for certain functionalities you need to add the permission to the Manifest file.

  1. Go to the Projects AndroidManifest.xml
  2. Click on the Permissions Tab
  3. Click on Add
  4. Select Uses Permission
  5. See the snapshot belowenter image description here

6.Save the manifest file and then run your project. Your project now should run as expected.

Rightwards answered 3/6, 2014 at 15:0 Comment(1)
Which IDE are you using?Grantham
B
1
11-25 14:47:01.681: ERROR/AndroidRuntime(302): blah blah...requires android.permission.CALL_PHONE

^ The answer lies in the exception output "requires android.permission.CALL_PHONE" :)

Bullheaded answered 2/9, 2014 at 13:5 Comment(0)
F
1
    final public void Call(View view){

    try {

        EditText editt=(EditText)findViewById(R.id.ed1);
        String str=editt.getText().toString();
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+str));
        startActivity(intent);
    }
    catch (android.content.ActivityNotFoundException e){

        Toast.makeText(getApplicationContext(),"App failed",Toast.LENGTH_LONG).show();
    }
Fiche answered 27/7, 2016 at 6:16 Comment(0)
H
1

use this code in Kotlin

fun makeCall(context: Context, mob: String) {
        try {
            val intent = Intent(Intent.ACTION_DIAL)

            intent.data = Uri.parse("tel:$mob")
            context.startActivity(intent)
        } catch (e: java.lang.Exception) {
            Toast.makeText(context,
                "Unable to call at this time", Toast.LENGTH_SHORT).show()
        }
    }
Hylozoism answered 9/2, 2021 at 5:53 Comment(1)
Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotesWive
A
0

String phone = "9553290143";

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));

startActivity(intent);

Antimicrobial answered 1/8, 2022 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.