2

I'm trying to implement Marshmallow's permission support, but my code inside "onRequestPermissionsResult" is never called. The "onCreate" and "onMapReady" parts of the code work fine. Checking if the device has the permission and requesting the permission works fine.

I'm using an Nexus 5X emulator running Android Marshmallow 6.0, API 23.

I have tried to do what it's mentioned here -> Android M Permissions: onRequestPermissionsResult() not being called

But I cannot get it to work, it doesn't matter if I use "ActivityCompat.requestPermissions" or directly "requestPermissions", it nevers get called.

I also updated my appcompat and support libraries to v24.2.0

Any ideas?

This is my activity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
UiSettings mapSettings;
int MY_LOCATION_REQUEST_CODE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //Check location permission for sdk >= 23
    if (Build.VERSION.SDK_INT >= 23) {

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            // Request permission.
            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_LOCATION_REQUEST_CODE);
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_LOCATION_REQUEST_CODE) {
        if (permissions.length == 1 &&
                permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            // Permission was denied. Display an error message.
        }
    }
}

}

Systemallica
  • 106
  • 1
  • 10

3 Answers3

1

MY_LOCATION_REQUEST_CODE should be a non-egative constant.

You should declare it as

private static final int MY_LOCATION_REQUEST_CODE = 1;
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Good catch, I thought this works only for startActivityForResult but request permissions obviously works the same way. Shame that docs doesn't mention this. – Alexander M. Sep 02 '16 at 18:16
  • Permission requests use `startActivityForResult` under the hood, so the same restrictions apply to both as the [docs say](https://developer.android.com/reference/android/app/Activity.html#requestPermissions(java.lang.String[],%20int)) - the same language is used for both. – ianhanniballake Sep 02 '16 at 18:18
  • 1
    ActivityCompat docs doesn't have the note `Should be >= 0.` : https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html – Alexander M. Sep 02 '16 at 18:21
  • @AlexanderMayatsky - good catch. Filed an internal bug to get the Javadoc updated for `ActivityCompat`. – ianhanniballake Sep 02 '16 at 18:30
1

I have the same issue, the issue was only on API 23 (Android M). And that because I was using the flag android:noHistory="true" in the manifest when declaring the activity:

<activity   android:name=".view.activity.SplashScreenActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

That because when showing the permission dialog the activity will be finished by the system because of that flag.

Removing it, or setting it to false fixes my issue.

Mahmoud
  • 2,683
  • 1
  • 30
  • 32
0

This line will never evaluate to true:

permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION

Because you are comparing these two strings by reference.

You should use TextUtils.equals()

TextUtils.equals(permissions[0], android.Manifest.permission.ACCESS_FINE_LOCATION)
DavidE
  • 181
  • 1
  • 5