onRequestPermissionsResult returns immediately wit

2019-06-23 10:13发布

My Android app needs to request permission for location services. I do this with:

ActivityCompat.requestPermissions(this, new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION},
                            REQUEST_CODE_ACCESS_COARSE_LOCATION);

But immediately after this is called, onRequestPermissionsResult returns immediately with permission denied:

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

        }
    }
}

The permission is listed in the manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>

I also cleared the cache to prevent any previous denial the user gave from affecting this.

I am expecting a dialog to pop up requesting the user to grant or deny permission to the location services but it isn't showing.

2条回答
神经病院院长
2楼-- · 2019-06-23 10:47
private int PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 100;  
private void checkPermission() {
     if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
             != PackageManager.PERMISSION_GRANTED) {
          requestPermissions(newString[{Manfest.permission.ACCESS_COARSE_LOCATION},
                  PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
      } else {
          doShowLocation();
      }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions,
          int[] grantResults) {
      if (requestCode == PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION
              && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          showContacts();
     }
  }
查看更多
我想做一个坏孩纸
3楼-- · 2019-06-23 10:49

Change:

<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>

to:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

The permission is for "coarse" location data (i.e., not fine-grained), not "course" location data (e.g., where some university class is being held).

查看更多
登录 后发表回答