Permissions when updating an app - android

I am developing an Android application and I have uploaded it to Google Play. When installing the application request for the camera, storage, etc. But when doing an update, it asks me again about the permission again.
Example camera permission:
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
return false;
}
return true;
}
private void requestCameraPermission() {
Log.w(TAG, "Camera permission is not granted. Requesting permission");
final String[] permissions = new String[]{Manifest.permission.CAMERA};
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
return;
}
Is there any way to avoid this?? Perhaps by code I can ask if the app is updating, that you do not ask for them again.

Related

Running an Activity that requires permission to access the gallery

I have this activity that is supposed to access the gallery and choose an image. It run with zero errors but when pressing the "choose " button I got a Permission Denied response in accessing the gallery. What is the problem?
If you are target app marshmallow or above you need to add run time permission.
Try below code
Add Permission in your Manifest using below line
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And use below code for get run time permission
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
Permission result callback:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}
For more detail please prefer below link
https://developer.android.com/training/permissions/requesting
I hope this can help you!
Thank You.

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/

Android Request Permission Callback

I'm trying to implement Android permissions from https://developers.google.com/android/guides/permissions
A couple of ques:
1) The callback onRequestPermissionResult - where are the parameters String[] permissions and int[]grantResults from?
2) I keep getting null values for grantResults and String[] permissions
3) I have set the value of the const REQUEST_LOCATION to 0 - does it matter what the value of this const is?
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
} else {
// permission has been granted, continue as usual
Location myLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
One unrelated ques...when getting a google map API key is it required to set the SHA-1 value or is that optional and just limits the key to android.
Thanks.
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults)
Get your run time permission like this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
// Permission already Granted
//Do your work here
//Perform operations here only which requires permission
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
and if permission is not already granted override onRequestPermission Results like this
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted
//Do your work here
//Perform operations here only which requires permission
}
}
}
public class RequestUserPermission {
private Activity activity;
// Storage Permissions
private static final int REQUEST_PERMISSION = 1;
private static String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
public RequestUserPermission(Activity activity) {
this.activity = activity;
}
public boolean verifyStoragePermissions() {
// Check if we have write permission
int permissionWrite = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionRead = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
if ((permissionWrite != PackageManager.PERMISSION_GRANTED) ||
(permissionRead != PackageManager.PERMISSION_GRANTED)) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS,
REQUEST_PERMISSION
);
return false;
}else{
return true;
}
}
public void providePermission(){
verifyStoragePermissions();
}
public void forceFullOpenPermission(){
if(!verifyStoragePermissions()){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivityForResult(intent, REQUEST_PERMISSION);
}
}
In your Activity
RequestUserPermission requestUserPermission = new
RequestUserPermission(DashBoard.this);
requestUserPermission.verifyStoragePermissions();
If you want to forceful open permission
requestUserPermission.forceFullOpenPermission();
1) request code is the one you named it REQUEST_LOCATION with value of 0. permissions is the requests you have sent. for example you can request for Location permission and Read SMS permission at once so you will get them in permissions string array. and grantResults is the status of each requests for example if user accepts Location request it will be 1 and if he rejected Read SMS it will be 0
2)I think this means you have rejected the request
3)With that if you have requested multiple times from different places you can determine which result is for which request
and answer of your unrelated question: no it's optional and if you don't want other applications uses your key you have to set that
1) The callback onRequestPermissionResult - where are the parameters String[] permissions and int[]grantResults from?
permissions is the list of permissions you requested and grantResults are the results of those requests. So since you only requested one permission (ACCESS_FINE_LOCATION), you will get back ACCESS_FINE_LOCATION in permissions, and either PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED in grantResults
2) I keep getting null values for grantResults and String[] permissions
This means that the user did not click Accept or Decline in the permission dialog. Rather, they cancelled the dialog by clicking outside of it, or leaving the application.
3) I have set the value of the const REQUEST_LOCATION to 0 - does it matter what the value of this const is?
No, this can be anything

How to know if user allowed a Permission upon prompt [duplicate]

This question already has answers here:
Android marshmallow request permission?
(26 answers)
Closed 6 years ago.
When the permission Dialog pops up, it asks if I want to allow or deny a certain permission. Thing is, when I allow or deny, the action doesn't execute. How do I know if he allowed so I can execute an action upon accepting?
I tried:
int hasWriteExternalStoragePermission = ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalStoragePermission == PackageManager.PERMISSION_GRANTED) {
// my code
}
But it doesn't execute as it's "too late"
Try this way :
public boolean isStoragePermissionGranted()
{
if (Build.VERSION.SDK_INT >= 23)
{
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission is granted");
return true;
}
else
{
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else
{
//permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted <23");
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
else
{
Log.v(TAG,"Permission denied");
}
}
This way, If the permission is granted it would come here:
Log.v(TAG,"Permission is granted");
If permission is not already granted , it would come here:
Log.v(TAG,"Permission is revoked"); and try get the permission.
And if user grants the permission in runtime :
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
if the user denies it :
Log.v(TAG,"Permission denied");
In case the version is below Marshamallow, ie below API 23, then it would come here:
Log.v(TAG,"Permission is granted <23");

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);
}
}

Categories

Resources