Search code examples
javaandroidmobilegeolocation

Java Android - Requesting multiple permissions but one doesn't get requested


So i'm trying to request both Location and Camera permissions from the user, and my app all works apart from the part where it has to request Location permissions from the user. The user never gets a request to access location. Is it even possible to request 2 different permissions and have onRequestPermissionsResults to handle them both in the same class?

 @Override
                                              public void onClick(View v) {
                                                  if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                                                          != PackageManager.PERMISSION_GRANTED) {
                                                      ActivityCompat.requestPermissions(
                                                              HomePageActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION_PERMISSION
                                                      );
                                                  } else {
                                                      getCurrentLocation();
                                                  }

                                                  askCameraPermissions();


                                              }

this is how it starts, then I think it goes here

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

        switch (requestCode) {
            case REQUEST_CODE_LOCATION_PERMISSION:

                if (grantResults.length > 0) {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        getCurrentLocation();

                    } else {
                        Toast.makeText(this, "Permission denied", Toast.LENGTH_LONG).show();
                    }
                }
                break;

            case CAMERA_PERM_CODE:
                    //if both are true then user has granted requests
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        dispatchTakePictureIntent();

                    } else {
                        Toast.makeText(this, "Camera permission is required to Use camera.", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }



        }

'''

except I believe this is where the application is doing something wrong. The camera request and permission works perfectly and I am able to continue and have my camera methods work. However when it comes to Location permissions, as i said, the user is never prompted to allow permissions, so I think something is going wrong here. Any help is appreciated, this is my first post so maybe formatting will be strewn with errors.

I have tried to post only relevant code. I will post more if requested


Solution

  • Rather then asking permission two times which is causing the problem you can actually ask it in a single time like

    ....
    ActivityCompat.requestPermissions(                                                              
        HomePageActivity.this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA}, 
        REQUEST_CODE_MULTIPLE_PERMISSION
    );
    ....
    

    if you still want to ask separate permission then you should ask after the first one result has arrived.

    EDIT

    while asking for access fine location you should also ask for coarse location