How to make a phone call using intent in Android?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to make a phone call using intent in Android?



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 ProcessRecord43d32508 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)




19 Answers
19



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.


Intent.ACTION_DIAL


Intent.ACTION_CALL


ACTION_DIAL


CALL_PHONE



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





+1 for "tel:" . I had call instead and got no intent exception. Tnx
– Tina
Jul 26 '14 at 10:11





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?
– MatheusJardimB
Sep 4 '15 at 14:00





What's the difference between the code in the question and in this answer? How does it solve the problem?
– Gavriel
Mar 3 '16 at 23:03



More elegant option:


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





This is great and should be the accepted answer
– Sapher
Jun 9 '17 at 6:47





This works without adding anything to Manifest like the previous answer
– Pavlos
Jul 11 '17 at 18:04





No runtime permissions required. +1
– kike
Jun 12 at 12:08



Every thing is fine.



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



and now every thing is working fine.





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.
– Ridcully
Jan 27 '13 at 18:46





Also, be aware of telephony-less devices : stackoverflow.com/a/9300036/693752
– Snicolas
Apr 24 '13 at 7:02






further info as suggested by Snicolas londatiga.net/it/programming/android/…
– Lorensius W. L. T
Feb 12 '14 at 0:16





This wont work for android Marshmallow.
– JehandadK
Jul 20 '16 at 10:53



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);





This is better in case where you don't need to ask permission.
– zackygaurav
Mar 5 at 9:08





There's a missing semicolon in the second line of the code. perfect answer!
– ahmed_khan_89
Jun 15 at 11:51



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)));



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);





This technique worked for me, although I had to change Intent.ACTION_DIAL to Intent.Anction_CALL.
– Naeem A. Malik
May 12 '14 at 13:08



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();





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.
– Mostrapotski
Jul 18 '16 at 21:04



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


Intent.ACTION_CALL


CALL_PHONE



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


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


<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.


CALL_PHONE



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.



Permissions:


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



Intent:


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



You can use this as well:


String uri = "tel:" + posted_by.replaceAll("[^0-9|\+]", "");



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();




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



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



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





Which IDE are you using?
– SHA2NK
Aug 9 '17 at 2:19


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" :)


requires android.permission.CALL_PHONE


if(ContextCompat.checkSelfPermission(
mContext,android.Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions((Activity) mContext, new
Stringandroid.Manifest.permission.CALL_PHONE, 0);
else
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Number")));



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();


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();



I think you just need to add a permission in your manifest:


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



this will get rid of your security exception.





This permission has nothing to do with dialing.
– Theo
Oct 11 '15 at 8:36





Irony is his issue was with the Permissions itself :D
– Ujju
Feb 9 '16 at 9:32





FYI, if you remove this answer you will get back all the reputation you lost due to downvotes.
– miken32
Aug 23 '16 at 19:37





@element11 fixed, thanks. Never seen an answer with so many downvotes, figured he wasn't aware of being able to delete!
– miken32
Aug 23 '16 at 19:39





wow it worked like a charm :P
– Learner
Sep 13 '16 at 14:08





Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard