-5

正在开发 android 运行时权限。我已经完成了所有部分。但我对一件事感到沮丧。

我想知道特定的许可已经被拒绝了,再也不会问了。

当然,一旦我们在下面的 API 中调用它,我们就可以得到结果

 ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);

我们可以在这里得到结果

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {

    if (requestCode == REQUEST_CAMERA) {
         // here we can get the result
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

但我的需要是不请求 requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); API 我想找到状态不再询问是否检查

如果有人知道,请分享这些东西。

4

2 回答 2

2

但我的需要是不请求 requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); API 我想找到状态不再询问是否检查

这将是困难的。

你可以打电话shouldShowRequestPermissionRationale()ActivityCompat如果出现以下情况,这将返回true

  • 您之前已向用户请求此权限
  • 用户拒绝了该权限
  • 用户选中“不再询问”复选框

你可以打电话checkSelfPermission()ContextCompat如果出现以下情况,这将返回true

  • 您之前已向用户请求此权限
  • 用户授予该权限

您还可以跟踪自己之前是否曾向用户请求过此权限,例如将该信息存储在SharedPreferences.

如果:

  • 您知道您之前已(通过SharedPreferences)请求此权限,并且
  • checkSelfPermission()返回false(因此用户没有授予权限),并且
  • shouldShowRequestPermissionRationale()返回false(因此您不应该向用户提供理由)

然后权限处于“不再询问”状态。

于 2015-12-18T14:06:18.163 回答
0

我有同样的问题,我想通了。为了让生活更简单,我编写了一个 util 类来处理运行时权限。

public class PermissionUtil {
    /*
    * Check if version is marshmallow and above.
    * Used in deciding to ask runtime permission
    * */
    public static boolean shouldAskPermission() {
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
    }
private static boolean shouldAskPermission(Context context, String permission){
        if (shouldAskPermission()) {
            int permissionResult = ActivityCompat.checkSelfPermission(context, permission);
            if (permissionResult != PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }
public static void checkPermission(Context context, String permission, PermissionAskListener listener){
/*
        * If permission is not granted
        * */
        if (shouldAskPermission(context, permission)){
/*
            * If permission denied previously
            * */
            if (context.shouldShowRequestPermissionRationale(permission)) {
                listener.onPermissionPreviouslyDenied();
            } else {
                /*
                * Permission denied or first time requested
                * */
if (PreferencesUtil.isFirstTimeAskingPermission(context, permission)) {
                    PreferencesUtil.firstTimeAskingPermission(context, permission, false);
                    listener.onPermissionAsk();
                } else {
                    /*
                    * Handle the feature without permission or ask user to manually allow permission
                    * */
                    listener.onPermissionDisabled();
                }
            }
        } else {
            listener.onPermissionGranted();
        }
    }
/*
    * Callback on various cases on checking permission
    *
    * 1.  Below M, runtime permission not needed. In that case onPermissionGranted() would be called.
    *     If permission is already granted, onPermissionGranted() would be called.
    *
    * 2.  Above M, if the permission is being asked first time onPermissionAsk() would be called.
    *
    * 3.  Above M, if the permission is previously asked but not granted, onPermissionPreviouslyDenied()
    *     would be called.
    *
    * 4.  Above M, if the permission is disabled by device policy or the user checked "Never ask again"
    *     check box on previous request permission, onPermissionDisabled() would be called.
    * */
    public interface PermissionAskListener {
/*
        * Callback to ask permission
        * */
        void onPermissionAsk();
/*
        * Callback on permission denied
        * */
        void onPermissionPreviouslyDenied();
/*
        * Callback on permission "Never show again" checked and denied
        * */
        void onPermissionDisabled();
/*
        * Callback on permission granted
        * */
        void onPermissionGranted();
    }
}

PreferenceUtil方法如下。

public static void firstTimeAskingPermission(Context context, String permission, boolean isFirstTime){
SharedPreferences sharedPreference = context.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE;
 sharedPreference.edit().putBoolean(permission, isFirstTime).apply();
 }
public static boolean isFirstTimeAskingPermission(Context context, String permission){
return context.getSharedPreferences(PREFS_FILE_NAME, MODE_PRIVATE).getBoolean(permission, true);
}

现在,您只需要使用带有正确参数的方法 * checkPermission*。

这是一个例子,

PermissionUtil.checkPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    new PermissionUtil.PermissionAskListener() {
                        @Override
                        public void onPermissionAsk() {
                            ActivityCompat.requestPermissions(
                                    thisActivity,
              new String[]{Manifest.permission.READ_CONTACTS},
                            REQUEST_EXTERNAL_STORAGE
                            );
                        }
@Override
                        public void onPermissionPreviouslyDenied() {
                       //show a dialog explaining permission and then request permission
                        }
@Override
                        public void onPermissionDisabled() {
Toast.makeText(context, "Permission Disabled.", Toast.LENGTH_SHORT).show();
                        }
@Override
                        public void onPermissionGranted() {
                            readContacts();
                        }
                    });

我想知道特定的权限已经被拒绝了,再也不会问了。

如果用户选中Never ask again,您将在onPermissionDisabled上收到回调。

快乐编码:)

于 2016-11-16T18:06:14.643 回答