Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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) 
like image 927
UMAR-MOBITSOLUTIONS Avatar asked Nov 25 '10 09:11

UMAR-MOBITSOLUTIONS


People also ask

How is intent used to dial numbers?

Intent phoneIntent = new Intent(Intent. ACTION_CALL); You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.

How can make call in Android programmatically?

Method 1) Dial a number (Permission-free) parse() function. Step 2: Set the Intent Action of Intent to Intent. ACTION_DIAL such that Android system can filter and delegate the dialling action to any app that can make a phone call.

How do you make a phone call with an app?

android:text = "Make Call!!" In Main activity Intent object is created to redirect activity to call manager and action attribute of intent is set as ACTION_CALL. Phone number input by user is parsed through Uri and that is passed as data in Intent object which is than use to call that phone number.


2 Answers

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.

like image 177
Ridcully Avatar answered Oct 08 '22 10:10

Ridcully


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.

like image 25
Denny Sharma Avatar answered Oct 08 '22 08:10

Denny Sharma