gpt4 book ai didi

java - 如何处理被拒绝的权限 Android M (EasyPermissions)

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:17 25 4
gpt4 key购买 nike

我正在使用 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)

我的难题是在哪里调用此函数,因为请求权限方法不返回任何内容(无效)。我试过类似的东西

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 );
}

但它总是在启动时运行被拒绝的权限,而不是当用户在运行时实际单击从不按钮时。任何帮助表示感谢谢谢..

EasyPermissions 链接 https://github.com/googlesamples/easypermissions

最佳答案

如果您不想使用 Easy Permissions,请完整解释每种权限情况

/**
* 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.
}

}
}

关于java - 如何处理被拒绝的权限 Android M (EasyPermissions),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40161547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com