I'm trying to programmatically call to a number with following code:
String number = ("tel:" + numTxt.getText());
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
I've set the permission in the Manifest:
<uses-permission android:name="android.permission.CALL_PHONE"/>
I'm working with real device for testing and debugging, it is Nexus 5 with Android M, my compileSdkVersion is 23. I'm getting the following Security Exception:
error: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{cbbd7c1 5228:com.dialerTest.DialerApp/u0a96} (pid=5228, uid=10096) with revoked permission android.permission.CALL_PHONE
I've searched the web and this community for similar Q/A and couldn't find the answer. Any help will be appreciated.
in fragment class
Step 1: import static android.Manifest.permission.CALL_PHONE;
step 2:Where your onclick button:
[Or] you are using in activity class means change the getActivity to getApplicationContext()
In android 6.0 (Api lvl 23) we have something called "Runtime Permissions". You have to read about it.
You can find documentation here.
You code could works only if you make an
ACTION_DIAL
, not anACTION_CALL
where you need to request a permission, so if you want to make a call, please follow this example:MANIFEST:
Code:
Permission CALL_PHONE belong to dangerous permission group.
So if your apps target SDK is 23 or higher and your device is running on Android 6.0 or higher, you must request for CALL_PHONE permission while the app is running.
Example :
When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult() method, passing it the user response.