Android permissions for Lollipop - android

Problem is, even when I refuse to give permission for ACCESS_COARSE_LOCATION in dialog, when I check permission in another activity of this app, it returns PERMISSION_GRANTED. It happens in android 5(Lollipop). Could you give some advice, why it's happening?
I check permission by this way:
int permissionCheck = ContextCompat.checkSelfPermission(Browser.this,
android.Manifest.permission .ACCESS_COARSE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
}else{
}

you have to check if current sdk is below marshmallow
if (Build.VERSION.SDK_INT >= 23) {
int permissionCheck = ContextCompat.checkSelfPermission(Browser.this,
android.Manifest.permission .ACCESS_COARSE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
}else{
}
} else {
// permission alredy granted
}

Related

How to re-request BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions for Android 12 after a user denial?

I have a problem re-requesting the permissions required to scan and connect to bluetooth devices when targeting SDK 31 (Android 12).
I call this method inside my main activity's onCreate():
public void requestBluetoothPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
|| (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {
Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");
this.requestPermissions(new String[]{
Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
}, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);
}
}
}
It's works fine the first time it is called i.e. an Android pop-up is displayed to the user, prompting him to grant the permissions.
But if he refuses to grant the permissions, next time onCreate() is called, the pop-up will not be displayed, which means the user remains unable to grant the permissions.
Any idea why and how to fix this ?
It appears Android 12 blocks requesting the same permission after user denied it once only.
Therefore, I ended up using ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.BLUETOOTH_SCAN) to determine wether the permission can be requested or not, in which case a snackbar message is displayed explaining why is the permission needed, with a button opening the app settings where permission can be granted.
Here is a sample of the code:
public void requestBluetoothPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
|| (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.BLUETOOTH_SCAN)) {
// display permission rationale in snackbar message
} else {
Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");
this.requestPermissions(new String[]{
Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
}, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);
}
}
}
}

android Build.GetSerial() throwing exception

I'm developing a Xamarin.Android project,where I need to get the Device Serial number.
I have implemented it the way it is shown below.
Also added the permissions in the manifest file:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
string serial;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
serial = Build.GetSerial();
}
else
{
serial = Build.Serial;
}
I have tried it on two different devices(both android 9.0).Sadly i get the following exception when the GetSerial() function is called(huawei p10): Java.Lang.SecurityException: <Timeout exceeded getting exception details> .
On an other device(galaxy s8) I get this exception:
Java.Lang.SecurityException: getSerial requires READ_PHONE_STATE or
READ_PRIVILEGED_PHONE_STATE permission
I really dont understand what the problem is,because I have added both permissions in the manifest,which the exception sais...
Any idea what am I doing wrong?
You should faced a persmission issue. And since Android Marshmallow, you need to ask the user for the permissions.
Besides adding the permission in android manifest file, you can also add runtime permissions like this:
static readonly int REQUEST_PHONE_STATE = 1;
public void checkPermission()
{
Log.Info(TAG, "Checking permission.");
// Check if the permission is already available.
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneState) != (int)Permission.Granted)
{
// permission has not been granted
RequestPhoneStatePermission();
}
else
{
// permissions is already available, show the camera preview.
Log.Info(TAG, " permission has already been granted.");
getInfo();
}
}
Method RequestPhoneStatePermission
private void RequestPhoneStatePermission()
{
Log.Info(TAG, "PhoneState permission has NOT been granted. Requesting permission.");
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadPhoneState))
{
Log.Info(TAG, "Displaying PhoneState permission rationale to provide additional context.");
Snackbar.Make(layout, Resource.String.permission_phonestate_rationale,
Snackbar.LengthIndefinite).SetAction(Resource.String.ok, new Action<View>(delegate (View obj) {
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
})).Show();
}
else
{
// PhoneState permission has not been granted yet. Request it directly.
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
}
}
Method OnRequestPermissionsResult
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if (requestCode == REQUEST_PHONE_STATE)
{
// Received permission result for camera permission.
Log.Info(TAG, "Received response for phone state permission request.");
// Check if the only required permission has been granted
if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
{
// Camera permission has been granted, preview can be displayed
Log.Info(TAG, "phonestate permission has now been granted. Showing preview.");
Snackbar.Make(layout, Resource.String.permission_available_phonestate, Snackbar.LengthShort).Show();
getInfo();
}
else
{
Log.Info(TAG, "phonestate permission was NOT granted.");
Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
method getInfo
private void getInfo() {
string serial;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
serial = Build.GetSerial();
}
else
{
serial = Build.Serial;
}
Log.Info(TAG, "serial = " + serial);
}
Here is a full demo, you can check it.
After that, you can get the effect:
For more details,you can check:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows
https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

App crashes when requesting permission

I'm using this code to request SMS permission before running the method SendSMS(string 1, string 2);
but my app crashes before permission request takes place.
What's missing?
final private int REQUEST_CODE = 101;
private void SendCreditSMS() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS}, REQUEST_CODE);
} else {
SendSMS("181", "رصيد");
}
}
Try this,
final private int REQUEST_CODE = 101;
private void SendCreditSMS() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.SEND_SMS}, REQUEST_CODE);
} else
{
SendSMS("181", "رصيد");
}
}
else{
SendSMS("181", "رصيد");
}
}
The problem may be due to the reason that you declared the activity in which you request permission(s) as with no history. To solve the problem remove
android:noHistory="true"
line from the associated activity's code block in AndroidManifest file. To have an activity with no history you can use other calls such as finishAndRemoveTask().
Try going to the application manager in your android device and select the app you are currently executing. Then check whether permissions are given by the device to use send_sms service in your device.

Automatically detect to show allow permission after user deny in the first time asking

I working with anroid app in Android M and need some permission that allow to access CAMERA, RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, ... so I put my permission checking when user open the app in the first time. But if user deny it, how can I detect it automatically that user is using the feature that required permission then ask them again? Or I have to put my condition in every feature that need permission.
I know when we use some feature that required permission but not allowed already it will throw the exception, so do we have any class that handle this task?
I use this logic: Every Activity extends a BaseActivity, in which there is a method that check the permissions everytime the onCreate() is called.
The method that I use is:
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
So, everytime the user launch a new Activity, the application check the permissions and display which permissions are not granted yet.
To manage the result, use onRequestPermissionsResult()
BTW, this logic will ask the user the permission even if the activity's feature doesn't require any permission.
If you want to ask the user the permissions only where the feature that requires a permission, you must check it in every activity
onRequestPermissionsResult you can check particular Permission is granted or not you can call Permission dialog again from there if particular permission not granted for Example
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
// Ask again for permission
}
// END_INCLUDE(permission_result)
} else if (requestCode == REQUEST_CONTACTS) {
Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be
// checked.
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted, display contacts fragment.
Snackbar.make(mLayout, R.string.permision_available_contacts,
Snackbar.LENGTH_SHORT)
.show();
} else {
Log.i(TAG, "Contacts permissions were NOT granted.");
// Ask again for permission
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Requesting Location Permission at Runtime

I have a query implementing RuntimePermission for Location. When I tried to requestLocationUpdates, I got LintError suggesting me to add PermissionCheck for that line. Considering that I implemented run-time permissions. So this is how it looks,
if (isNetworkEnabled() && networkListener != null) {
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]
{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
}
And my main class implements onRequestPermissionsResult callback. This looks like,
switch (requestCode) {
case REQUEST_LOCATION:
if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
} else
Message.displayToast(context, "Without enabling permission, you can't access this feature");
break;
}
After the permission is granted, I request location updates again. But it again shows LintError to add the PermissionCheck. Refer the below image
Just for try I checkSelfPermission before requesting for requestLocationUpdate inside onRequestPermissionsResult and the error is gone. Like below code.
if (ActivityCompat.checkSelfPermission(context, permissions[0]) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(context, permissions[1]) == PackageManager.PERMISSION_GRANTED)
So, my question is do I need to check the permission once again if the user granted the permission? Correct me if I'm wrong!
You do need to check for checkSelfPermission because with latest OS 6 (Marshmallow) you can revoke the permissions granted for an app by going into settings.
So even if user has granted the permissions to app during installation time, at runtime you need to doublecheck whether your app still has the permissions or user has revoked those permissions.

Categories

Resources