Android 位置权限

Android Location Permission

我在我的应用程序中使用了这两个权限

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

我已经在清单中添加了权限。但是,当我调试 phone 时,我尝试使用以下函数检查权限:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Log.i("permission","done");
    }
    else
    {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSION_ACCESS_COARSE_LOCATION);
        }
        // The ACCESS_FINE_LOCATION is denied, then I request it and manage the result in
        // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this,
                    new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                    MY_PERMISSION_ACCESS_FINE_LOCATION);
        }

    }

应用程序将输入第一个 IF 语句,而不是向用户请求权限。 我之前没有授予此应用程序任何权限。 为什么即使我只是在 phone 中调试和安装应用程序,权限也会变为 GRANTED?

因为运行时权限是在 Marshmallow 中添加的,之前的版本没有,所以如果你在清单中添加了权限,它会自动授予权限,甚至不会在运行时请求权限,除非它是 Marshmallow 设备.