android marshmallow:如何知道用户是否拒绝一个应用程序权限?

android marshmallow: how to know if user deny one app permissions?

在 Android,我检查该位置是否已启用

LocationManager.isProviderEnabled(GPS_PROVIDER) || LocationManager.isProviderEnabled(NETWORK_PROVIDER)

这个工作正常,但是在棉花糖(和上层)上,当用户进入应用程序设置并仅拒绝我的应用程序使用该位置的权限时(仅针对我的应用程序,就像现在允许的 mashmallow 一样)然后之前的请求还是returntrue

我也试试 :

MyActivity.checkSelfPermission('android.permission.ACCESS_FINE_LOCATION') == PERMISSION_GRANTED or MyActivity.checkSelfPermission('android.permission.ACCESS_COARSE_LOCATION') == PERMISSION_GRANTED

但它总是 return 正确,即使用户拒绝对我的应用程序的权限

确保在 build.gradle 中将 compileSdkVersiontargetSdkVersion 设置为 23。如果低于 23,应用程序将使用旧的权限方法,您提到的方法将不起作用。

尝试使用以下代码:

ContextCompat.checkSelfPermission(thisActivity,   Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED

checkSelfPermission 不是 return 布尔值。它 return 是一个整数。

这是检查权限的正确方法:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {

    // permission is not granted, request it
    ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

} else {    
    //permision granted
}

这就是处理它们的方式

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_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
                // contacts-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
    }
}

来自文档

https://developer.android.com/training/permissions/requesting.html

请参考此link。确保 compileSdkVersion 应该是 23 岁及以上。

我在这里创建了检查多个运行时权限的代码,should show rational。