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.
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 :
String number = ("tel:" + numTxt.getText());
mIntent = new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(number));
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
// MY_PERMISSIONS_REQUEST_CALL_PHONE is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
//You already have permission
try {
startActivity(mIntent);
} catch(SecurityException e) {
e.printStackTrace();
}
}
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.
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the phone call
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
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 an ACTION_CALL where you need to request a permission, so if you want to make a call, please follow this example:
MANIFEST:
<uses-permission android:name="android.permission.CALL_PHONE" />
Code:
import static android.Manifest.permission.CALL_PHONE;
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:0612312312"));
/*
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:0612312312"));
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}*/
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(i);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
in fragment class
Step 1:
import static android.Manifest.permission.CALL_PHONE;
step 2:Where your onclick button:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +driver_no ));
if (ContextCompat.checkSelfPermission(getActivity(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
[Or] you are using in activity class means change the getActivity to getApplicationContext()
FYI: If you're targeting Android 11, you need to add an Intent Query element for Intent.ACTION_DIAL in your manifest.
https://developer.android.com/training/basics/intents/package-visibility#intent-signature
Related
I'm trying to create a calling button in Android. I created one and it works in all kind of Android device but it's not working on Marshmallow. I don't know why. How to create a calling button for android Marshmallow?
This is the code I used for the calling button :
public void button(View v) {
String number = "12354656";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent);
}
You'll need to enable permission for your app in the phone settings. Look under permissions in settings
The issue is probably related to Runtime Permissions introduced in Android 6.0.
You can try this:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
You have to try this...
put in your android manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
get runtime permission for make phone call(Android API version 23 and above)
private static final int REQUEST_PERMISSION_CALL = 111;
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
} else {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
}
}
you will get permission response on
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_CALL) {
// put your code here to make call
}
}
then you have to make your phone call
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
for more info https://developer.android.com/training/permissions/requesting.html
Hope this will help you.
This question already has answers here:
Android permission doesn't work even if I have declared it
(11 answers)
Closed 6 years ago.
Why i get this message
if the i set the permission in the manifest file:
Thank you for any help.
You need request permission at runtime in android >= 6.0
https://developer.android.com/training/permissions/requesting.html
You have to grant permission in android version >=6.0 (Marshmallow and above).
For Android 6.0+, you have to request the permission at runtime as well as adding it to the manifest. So something like this added to your Activity onCreate();
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.READ_CONTACTS);
//...add any others you need to this arraylist
// Convert the List to an Array
String[] permissionsArray = permissions.toArray(new String[0]);
ActivityCompat.requestPermissions(getActivity(), permissionsArray, REQUEST_CODE);
//The REQUEST_CODE is just a integer that your callback method will check, set it to some number
Then override this method in your activity to check whether the request was granted or not
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted, do your thing
} else {
//Permission denied, you can't do what you want to because user didn't give you permission
}
return;
}
}
See here for more info https://developer.android.com/training/permissions/requesting.html
Change your target sdk version to 22 in your app's build.gradle if you donot want to request the permission to the user at run time.
Else if you want to keep the target sdk version to 23 then you would need to request the permission from the user at run time as mentioned above by #wanpanman
Call this permission.
//Check permissions
public boolean reqPermission() {
if (!Permissions.checkPermission(this, Manifest.permission.READ_CONTACTS)) {
if (shouldShowRationale(Manifest.permission.READ_CONTACTS)) {
Snackbar.make(yourLayout, "Need to enable contact permission to use this feature. bla bla bla",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.settings, new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
})
.show();
return true;
} else {
Permissions.grantPermission(this, PERMISSION_REQUEST_CODE);
return true;
}
} else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean shouldShowRationale(String permission) {
return shouldShowRequestPermissionRationale(permission);
}
I'm trying to start a call intent action on a device who has Marshmallow as OS, Using the same steps as usual (This is working on versions below):
Add permission:
<uses-permission android:name="android.permission.CALL_PHONE" />
Open the intent:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + getString(R.string.connect_phone)));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This is the log I'm getting:
FATAL EXCEPTION: main
Process: com.app.calling, PID: 4250
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.intent.action.CALL dat=tel:xxxxxxxxxxxxx flg=0x10000000
cmp=com.android.server.telecom/.components.UserCallActivity
VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} }
from ProcessRecord{1618b01 4250:com.app.calling/u0a234} (pid=4250,
uid=10234) with revoked permission android.permission.CALL_PHONE
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at
android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130)
at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1540)
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at
android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849)
at
android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
at android.support.v4.app.Fragment.startActivity(Fragment.java:919)
at
com.app.calling.activity.fragment.ConnectFragment$2.onGroupClick(ConnectFragment.java:44)
at
android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:676)
at
android.widget.ExpandableListView.performItemClick(ExpandableListView.java:654)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3821)
at android.widget.AbsListView$3.run(AbsListView.java:5841)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Again, this process is working fine in the previous version (Lollipop and kitkat)unfortunately isn't on Marshmallow, does anybody know why or what I'm missing?
Method to make call
public void onCall() {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.CALL_PHONE},
"123");
} else {
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:12345678901")));
}
}
Check permission
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case 123:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
onCall();
} else {
Log.d("TAG", "Call Permission Not Granted");
}
break;
default:
break;
}
}
Beginning in android 6.0 (API 23), dangerous permissions must be declared in the manifest AND you must explicitly request that permission from the user. According to this list, CALL_PHONE is considered a dangerous permission.
Every time you perform an operation that requires a dangerous permission, you must check if that permission has been granted by the user. If it has not, you must request that it be granted. See Requesting Permissions at Run Time on Android Developers.
For Marshmallow version and above you need to ask the permission at runtime not only in the manifest file. Here is the documentation:
Requesting Permissions at Run Time
Hope it helps.
I'm developing an app with connectivity which connects to a Bluetooth device with SDK 23 as compile with. I'm having problems with requesting multiple permissions for Bluetooth. This is what I have done so far:
#Override
public void onStart() {
super.onStart();
if (D)
Log.e(TAG, "++ ON START ++");
if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
Manifest.permission.BLUETOOTH)
!= PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
REQUEST_ENABLE_BT);
}
if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
Manifest.permission.BLUETOOTH)
!= PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
REQUEST_CONNECT_DEVICE_INSECURE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ENABLE_BT: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (CommonData.mChatService == null)
setupChat();
Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
}
return;
}
case REQUEST_CONNECT_DEVICE_INSECURE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_CONNECT_DEVICE_INSECURE);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (CommonData.mChatService == null)
setupChat();
Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Although I'm able to get the dialogue box for requesting enabling the Bluetooth, I don't get the second permission, i.e. to connect to a device. In the logcat, I get:
01-01 06:41:24.334 25473-25473 E/BluetoothChat: ++ ON START ++
01-01 06:41:24.344 25473-25473 W/Activity: Can reqeust only one set of permissions at a time
And since I'm not able to connect to the device, I just get stuck here. And this code works fine on Android version up to Lollipop, just causes problem on the Marshmallow version.
BLUETOOTH and BLUETOOTH_ADMIN are normal permissions and are therefore they are automatically granted. Only permissions in the table of dangerous permissions need to requested at runtime.
However, as mentioned in the Android 6.0 changes: Access to Hardware Identifier:
To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:
WifiManager.getScanResults()
BluetoothDevice.ACTION_FOUND
BluetoothLeScanner.startScan()
If you're using any of those methods, you'll need to request at least ACCESS_COARSE_LOCATION at runtime (as it is a dangerous permission).
I am to new to Android 6.0 Coding Please Provide a solutions For the Below Code:
When i provide Run Time Permissions like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE it shows an Exception like
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Pictures/MyAppNew%20File%20Upload/IMG_20160401_110234.jpg} (has extras) } from ProcessRecord{ed96564 26955:com.social.nocializer/u0a259} (pid=26955, uid=10259) with revoked permission android.permission.CAMERA
Either MediaStore.ACTION_IMAGE_CAPTURE and MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE Run Time Permissions are not working...
Note: READ_EXTERNAL_STORAGE Works For When Opening Gallery
You have to manage run time permission for this, Because whichever permissions you have defined in AndroidManifest will not be automatically granted. So like below method you can check whether you permission is approved or not
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
Here, MY_REQUEST_CODE is a static constant that you can define, which will be used again for the requestPermission dialog callback. Now, You will need a callback for the dialog result:
#Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
#Ronak Solution worked for me but with a few following changes, As we need to check on only those devices which are above Android M.
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
And override the following method using crl+o copying, pasting would might result in an error :D
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}