I am trying to request ACCESS_FINE_LOCATION permissions to get the current location of the user.
My log shows that my application does not currently have this permission when querying ContextCompat.checkSelfPermission() , but when calling ActivityCompat.requestPermissions() nothing is displayed.
My Google map code (implementation of OnMapReadyCallback and ActivityCompat.OnRequestPermissionsResultCallback() ) is in FragmentActivity .
I managed to get the requestPermissions() function that successfully works in other actions in the application, this is only the one with the Google map. It does not work if it is placed in the onCreate() method of Activity or in onMapReady() (where it should go).
if(ContextCompat.checkSelfPermission(LocationActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "not granted"); final String[] permissions = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}; if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) { Log.d(TAG, "rationale"); // Explain to the user why permission is required, then request again AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("We need permissions") .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1); } }); AlertDialog alert = builder.create(); alert.show(); } else { Log.d(TAG, "request" + android.Manifest.permission.ACCESS_FINE_LOCATION); // If permission has not been denied before, request the permission ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1); } } else { Log.d(TAG, "granted"); }
Any ideas? Does this have something to do with my Activity ( FragmentActivity ) class, or is it possible that the Google map calls the permission request asynchronously?
java android android-permissions android-fragments google-maps
Robin May 10 '16 at 13:50 2016-05-10 13:50
source share