0

I am trying to build an android app with an AppCompatActivity that needs fine location permission after log in. I have managed to configure permission request and and on permission result elsewhere in the app successfully for storage and directory access purposes.

However, in this particular case, I am encountering a weird behaviour which I am not able to debug, and googling/searching on S/O has also not given me what I am looking for.

Basically, after my requestPermissions() method executes and dialog to grant permission appears on screen, a second mysterious dialog is appearing that interrupts the flow of my code. This is causing all sorts issues, as expected.

My request permissions method:

private void requestPermissions() {
    Log.i(TAG, "Requesting permission");
    ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_PERMISSIONS_REQUEST_CODE);
}

The permission request dialog successfully opens: Permission request dialog

But then, inexplicably, this second dialog opens

Unexplained second dialog

Clicking 'OK' on this dialog takes me to the settings > permissions page:

App Permissions page

Ideally I would like to avoid this second dialog, as it blocks the flow my code returning to onPermissionRequestsResult method, and creates other cascading issues.

But I am completely unable to identify the source of this second pop up.

My onCreate method:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        getSupportActionBar().hide();

        Picasso.with(this).setLoggingEnabled(true);

        // Check if user has revoked permissions
        if (!checkPermissions()) {
            requestPermissions();
        }
    }

The checkPermissions method:

/**
 * Returns the current state of the permissions needed.
 */
private boolean checkPermissions() {
    return  PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION);
}

My manifest file has the following permissions declared:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.location.gps"
        android:required="true" />

These are all the parts of my code that I could think of for reproducing this issue. But seeing as I am completely unable to narrow down where this second dialog is cropping up from, I understand there might be more things one might need from. Please let me know if so.

wanderingProgrammer
  • 191
  • 1
  • 3
  • 19
  • If You deny the permission for location then dialogs come for allow the permission . – Bhagyashri Mar 12 '18 at 08:59
  • @userI I am sorry if I haven't made this clear, but the second dialog appears moments after the one I trigger through my code. The second one doesn't wait for me to deny permission. It appears before the user can take any action on the first dialog. – wanderingProgrammer Mar 12 '18 at 09:01
  • checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an // app-defined int constant return; } – Bhagyashri Mar 12 '18 at 09:10
  • @userI this isn't significantly different from the code I have shown above. In fact, considering I am using the support library, the code you have given wouldn't work. My app needs ActivityCompat.requestPermissions instead of just requestPermissions. – wanderingProgrammer Mar 12 '18 at 09:18
  • I have posted the code.If still theres any error please let me know – Bhagyashri Mar 12 '18 at 09:33

4 Answers4

0

what is the model of your phone ? meizu flyme? or something like, if it's flyme ,you can try to open the "Security" App , in the Permisssions config your app's location permission.

志威梦
  • 136
  • 3
0
public void checkPer()
{

    if ((ContextCompat.checkSelfPermission(MainActivity.this,"android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED)) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        }
    } else {
        // user already provided permission
        // perform function for what you want to achieve
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    boolean canUseExternalStorage = false;

    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                canUseExternalStorage = true;
            }

            if (!canUseExternalStorage) {
                Toast.makeText(MainActivity.this, "You cannot see images without requested permission", Toast.LENGTH_SHORT).show();
            } else {
                // user now provided permission
                // perform function for what you want to achieve

            }
        }
    }
}

This will help you.....

0

check this code

   public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;

    String TAG="ResourceActivity";

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        Log.d(TAG, "Permission callback called-------");
        switch (requestCode) {
            case REQUEST_ID_MULTIPLE_PERMISSIONS: {

                Map<String, Integer> perms = new HashMap<>();
                // Initialize the map with both permissions
                perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);


                // Fill with actual results from user
                if (grantResults.length > 0) {
                    for (int i = 0; i < permissions.length; i++)
                        perms.put(permissions[i], grantResults[i]);
                    // Check for both permissions
                    if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                            && perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        ) {
                        // Log.d(TAG, "sms & location services permission granted");
                        // process the normal flow
                        //else any one or both the permissions are not granted






                    } else {
                        Log.d(TAG, "Some permissions are not granted ask again ");
                        //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
//                        // shouldShowRequestPermissionRationale will return true
                        //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.GET_ACCOUNTS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                            showDialogOK(getString(R.string.all_permission),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            switch (which) {
                                                case DialogInterface.BUTTON_POSITIVE:
                                                    Log.d(TAG, "positive button");

                                                    checkAndRequestPermissions();
                                                    break;
                                                case DialogInterface.BUTTON_NEGATIVE:
                                                    // proceed with logic by disabling the related features or quit the app.

                                                    Toast.makeText(ResourceActivity.this, "enable permission", Toast.LENGTH_LONG).show();
                                                    finish();

                                                    break;
                                            }
                                        }
                                    });
                        }
                        //permission is denied (and never ask again is  checked)
                        //shouldShowRequestPermissionRationale will return false
                        else {

                            //                            //proceed with logic by disabling the related features or quit the app.
                        }
                    }
                }
            }
        }

    }

    private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", okListener)
                .create()
                .show();
    }


    private boolean checkAndRequestPermissions() {
        int permissionWritePermission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);


        List<String> listPermissionsNeeded = new ArrayList<>();
        if (locationPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }



        if (permissionWritePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }







   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkAndRequestPermissions()) {
    //true then proceed further
    }
}

If you still getting the same error let me know

Bhagyashri
  • 182
  • 1
  • 15
0

The following S/O answer solved my issue: https://stackoverflow.com/a/38431653/3438497

Basically, I had to move my permissions request to the launcher activity instead of waiting till the login action was completed. This completely fixed my issue.

wanderingProgrammer
  • 191
  • 1
  • 3
  • 19