ActivityCompat.requestPermissions does not display a tooltip

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?

+4
java android android-permissions android-fragments google-maps
May 10 '16 at 13:50
source share
4 answers

After completely deleting my class, and it still does not work, I realized that this operation is created using TabHost.

When I stop using TabHost, the prompt is displayed successfully. I think TabHosts are not supported by new permission requests - is this a bug?

Same issue as Application Requests not showing

In the end, I created PermissionsRequestActivity, which processes the permission request and response on behalf of my TabHost, and then shuts down (pass the requested permission information through the Intent package of additional tasks).
It returns a response to the request in the form of a broadcast that is picked up by my TabHost.

A bit of hacking, but it works fine!

+8
May 10 '16 at 15:16
source share

Make sure that you have already added the requested permission to the Android manifest file, for example, before Android M, only then you will get the expected behavior.

Add manifest permission so you can request it through ActivityCompat.requestPermissions:

 <uses-permission android:name="android.permission. ACCESS_FINE_LOCATION" /> 
0
May 22, '16 at 21:45
source share

I will share the code that works for me. In the protected void onCreate (Bundle savedInstanceState) {} method of my activity, where I want to see the invitation, I included this code:

  /* Check whether the app has the ACCESS_FINE_LOCATION permission and whether the app op that corresponds to * this permission is allowed. The return value is an int: The permission check result which is either * PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP. * Source: https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html * While testing, the return value is -1 when the "Your location" permission for the App is OFF, and 1 when it is ON. */ int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); // The "Your location" permission for the App is OFF. if (permissionCheck == -1){ /* This message will appear: "Allow [Name of my App] to access this device location?" * "[Name of my Activity]._instance" is the activity. */ ActivityCompat.requestPermissions([Name of my Activity]._instance, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION); }else{ // The "Your location" permission for the App is ON. if (permissionCheck == 0){ } } 

Before the protected void onCreate (Bundle savedInstanceState) {} method, I created the following constant and method:

 public static final int REQUEST_CODE_ACCESS_FINE_LOCATION = 1; // For implementation of permission requests for Android 6.0 with API Level 23. // Code from "Handle the permissions request response" at https://developer.android.com/training/permissions/requesting.html. @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_CODE_ACCESS_FINE_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // location-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } 
0
Mar 22 '17 at 20:28
source share

I ran into the same problem in a project that uses TabHost. @Robin Solution Basics, I use the EventBus library to send a message from a child activity to TabActity.

EventBus: https://github.com/greenrobot/EventBus

Create an event object:

 public class MessageEvent { private String message; public MessageEvent(String message){ this.message = message; } public String getMessage(){ return this.message; } } 

In your main business:

 private EventBus eventBus = EventBus.getDefault(); @Override protected void onCreate(Bundle savedInstanceState) { eventBus.register(this); } @Override protected void onDestroy() { eventBus.unregister(this); super.onDestroy(); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { if (event.getMessage().equals("contacts")){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainPage.this,new String[]{android.Manifest.permission.WRITE_CONTACTS}, 100 ); } } }; 

Specify another message for the permission you want to request. In your subsidiary activities you can send the appropriate message:

 EventBus.getDefault().post(new MessageEvent("contacts")); 

Keep in mind onRequestPermissionsResult callback and request code;)! He will work only in the main action.

0
Oct 20 '17 at 20:51 on
source share



All Articles