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)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With