如何处理被拒绝的权限 Android M (EasyPermissions)
How to Handle Denied Permissions Android M (EasyPermissions)
我正在使用 EasyPermissions 检查我的 android 中是否已授予某些权限,如果没有,则请求它们。很棒的库,效果很好,但我仍然没有弄清楚如果某些权限被拒绝如何处理。
所以基本上你 运行 创建一个这样的代码来检查
if (EasyPermissions.hasPermissions(Splash.this, perms )) {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = telephonyManager.getDeviceId();
String SimSimSerial = telephonyManager.getSimSerialNumber();
Toast.makeText(Splash.this, "IMEI: " + IMEI + " SimSerial: " + SimSimSerial, Toast.LENGTH_SHORT).show();
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
代码分解:如果存在权限,则继续其他请求,这很好。我的问题是,如果在请求期间有人点击永不询问按钮怎么办。 EasyPermissions 的人有一个函数
EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)
我的难题是在哪里调用此函数,因为请求权限方法没有 returns 任何内容(无效)。我试过类似
if (EasyPermissions.hasPermissions(Splash.this, perms )) {...
} else if (EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)) {
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
但它总是 运行 权限在开始时被拒绝,而不是当用户在 运行 时间内实际单击永不按钮时。任何帮助表示感谢谢谢..
link 到 EasyPermissions https://github.com/googlesamples/easypermissions
检查此 link。
在这里你必须实现EasyPermissions.PermissionCallbacks,你将被提供添加方法onRequestPermissionsResult, onPermissionsGranted、onPermissionsDenied。然后在 onPermissionsDenied 你可以处理你的拒绝状态。
试试看,让我知道它是否适合你。
如果您不想使用简单权限,请完整解释每种权限情况
/**
* Case 1: User doesn't have permission
* Case 2: User has permission
*
* Case 3: User has never seen the permission Dialog
* Case 4: User has denied permission once but he din't clicked on "Never Show again" check box
* Case 5: User denied the permission and also clicked on the "Never Show again" check box.
* Case 6: User has allowed the permission
*
*/
public void handlePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// This is Case 1. Now we need to check further if permission was shown before or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// This is Case 4.
} else {
// This is Case 3. Request for permission here
}
} else {
// This is Case 2. You have permission now you can do anything related to it
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This is Case 2 (Permission is now granted)
} else {
// This is Case 1 again as Permission is not granted by user
//Now further we check if used denied permanently or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// case 4 User has denied permission but not permanently
} else {
// case 5. Permission denied permanently.
// You can open Permission setting's page from here now.
}
}
}
我正在使用 EasyPermissions 检查我的 android 中是否已授予某些权限,如果没有,则请求它们。很棒的库,效果很好,但我仍然没有弄清楚如果某些权限被拒绝如何处理。
所以基本上你 运行 创建一个这样的代码来检查
if (EasyPermissions.hasPermissions(Splash.this, perms )) {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = telephonyManager.getDeviceId();
String SimSimSerial = telephonyManager.getSimSerialNumber();
Toast.makeText(Splash.this, "IMEI: " + IMEI + " SimSerial: " + SimSimSerial, Toast.LENGTH_SHORT).show();
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
代码分解:如果存在权限,则继续其他请求,这很好。我的问题是,如果在请求期间有人点击永不询问按钮怎么办。 EasyPermissions 的人有一个函数
EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)
我的难题是在哪里调用此函数,因为请求权限方法没有 returns 任何内容(无效)。我试过类似
if (EasyPermissions.hasPermissions(Splash.this, perms )) {...
} else if (EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)) {
} else {
EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. " ,PERMS_REQUEST_CODE, perms );
}
但它总是 运行 权限在开始时被拒绝,而不是当用户在 运行 时间内实际单击永不按钮时。任何帮助表示感谢谢谢..
link 到 EasyPermissions https://github.com/googlesamples/easypermissions
检查此 link。
在这里你必须实现EasyPermissions.PermissionCallbacks,你将被提供添加方法onRequestPermissionsResult, onPermissionsGranted、onPermissionsDenied。然后在 onPermissionsDenied 你可以处理你的拒绝状态。 试试看,让我知道它是否适合你。
如果您不想使用简单权限,请完整解释每种权限情况
/**
* Case 1: User doesn't have permission
* Case 2: User has permission
*
* Case 3: User has never seen the permission Dialog
* Case 4: User has denied permission once but he din't clicked on "Never Show again" check box
* Case 5: User denied the permission and also clicked on the "Never Show again" check box.
* Case 6: User has allowed the permission
*
*/
public void handlePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// This is Case 1. Now we need to check further if permission was shown before or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// This is Case 4.
} else {
// This is Case 3. Request for permission here
}
} else {
// This is Case 2. You have permission now you can do anything related to it
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This is Case 2 (Permission is now granted)
} else {
// This is Case 1 again as Permission is not granted by user
//Now further we check if used denied permanently or not
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// case 4 User has denied permission but not permanently
} else {
// case 5. Permission denied permanently.
// You can open Permission setting's page from here now.
}
}
}