【问题标题】:How to Handle Denied Permissions Android M (EasyPermissions)如何处理被拒绝的权限 Android M (EasyPermissions)
【发布时间】:2017-03-02 20:58:18
【问题描述】:

我正在使用 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 的 Fragment/Activity 中实现 EasyPermissions.PermissionCallbacks?
  • 不,请问我该如何调用它?认为这是可选的...
  • 是的,实现 EasyPermissions.PermissionCallbacks 然后它会要求你添加方法,然后你可以在那里处理你的拒绝
  • 有道理,我试试看

标签: java android permissions android-permissions


【解决方案1】:

检查这个link

在这里您必须实现 EasyPermissions.PermissionCallbacks,您将被提供添加方法,这些方法将是 onRequestPermissionsResultonPermissionsGranted、onPermissionsDenied。然后在 onPermissionsDenied 你可以处理你的拒绝状态。 试试看,让我知道它是否对你有用。

【讨论】:

  • 感谢楼主,这让我明白了很多。
  • 其实onRequestPermissionsResult不是由EasyPermissions.PermissionCallbacks定义的。这是继承自Activity(或支持Fragment 类)的方法。
【解决方案2】:

如果您不想使用 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.
        }

    }
}

【讨论】:

  • 很好的解释,如果我们想在显示权限对话框之前添加警告对话框。例如,如果用户没有权限并单击相机访问按钮,则显示一个对话框仅供参考,然后用户单击“确定”,然后将显示权限对话框。你能帮我解决这个问题吗?谢谢
  • @Navjot.jassal Watch case 4 请在那里您可以显示对话框,当用户同意对话框信息时,您可以请求许可
  • 您能否在代码中进行此更改,这将很有帮助。我很感激这个谢谢你
  • 已解决,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-03
  • 2015-12-21
  • 2020-06-03
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多