Android targetSdkVersion 23 checkSelfPermission 方法
Android targetSdkVersion 23 checkSelfPermission method
我在 Android 6.0 (API 23) 中面临检查权限值的问题。即使从应用程序的设置中启用或禁用权限,也始终获得 0 值。
以下是我采取的步骤。
从设备 settings->apps-> My apps-> permission -> Disable contact permission
手动禁用联系人权限。
仍然在 Android 6.0 中,每次执行以下代码行时都得到 0 值。
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
下面是我的代码。我在主启动器中定义 activity class
// Identifier for the permission request
private static final int WRITE_CONTACTS_PERMISSIONS_REQUEST = 9;
........
@Override
public void onCreate(Bundle savedInstanceState) {
....
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
{
sharedPreferencesEditor.putBoolean(getString(R.string.ALLOW_ACCESS_PHONEBOOK), true);
sharedPreferencesEditor.commit();
}
else {
getPermissionToReadUserContacts();
}
....
}
// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...) is only available
// in Marshmallow
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
if (shouldShowRequestPermissionRationale(
Manifest.permission.WRITE_CONTACTS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
WRITE_CONTACTS_PERMISSIONS_REQUEST);
}
}
// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == WRITE_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Write Contacts permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Write Contacts permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
如果您的目标是 SDK 23 (Android 6),所有权限(在您的清单中)都默认禁用,而如果您的目标 SDK 是 22 (Android 5.1) 并且您的应用是运行 on Android 6,用户安装应用程序时默认启用所有权限,即使用户稍后撤销权限,checkSelfPermission
returns PERMISSION_GRANTED
的值不正确
它也可以在 PermissionChecker
的文档中找到
In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.
我遇到了同样的问题,但我意识到存在目标版本低于 23 的本地模块。删除它们后,错误已解决。
我在 Android 6.0 (API 23) 中面临检查权限值的问题。即使从应用程序的设置中启用或禁用权限,也始终获得 0 值。
以下是我采取的步骤。
从设备 settings->apps-> My apps-> permission -> Disable contact permission
手动禁用联系人权限。
仍然在 Android 6.0 中,每次执行以下代码行时都得到 0 值。
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
下面是我的代码。我在主启动器中定义 activity class
// Identifier for the permission request
private static final int WRITE_CONTACTS_PERMISSIONS_REQUEST = 9;
........
@Override
public void onCreate(Bundle savedInstanceState) {
....
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
{
sharedPreferencesEditor.putBoolean(getString(R.string.ALLOW_ACCESS_PHONEBOOK), true);
sharedPreferencesEditor.commit();
}
else {
getPermissionToReadUserContacts();
}
....
}
// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...) is only available
// in Marshmallow
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
if (shouldShowRequestPermissionRationale(
Manifest.permission.WRITE_CONTACTS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
WRITE_CONTACTS_PERMISSIONS_REQUEST);
}
}
// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == WRITE_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Write Contacts permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Write Contacts permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
如果您的目标是 SDK 23 (Android 6),所有权限(在您的清单中)都默认禁用,而如果您的目标 SDK 是 22 (Android 5.1) 并且您的应用是运行 on Android 6,用户安装应用程序时默认启用所有权限,即使用户稍后撤销权限,checkSelfPermission
returns PERMISSION_GRANTED
它也可以在 PermissionChecker
的文档中找到In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.
我遇到了同样的问题,但我意识到存在目标版本低于 23 的本地模块。删除它们后,错误已解决。