开发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 我想找到状态永远不再问是否检查
如果有人知道的话请分享。
答案 0 :(得分:2)
但是我的需求是没有请求requestPermissions(this,PERMISSIONS_CONTACT,REQUEST_CONTACTS); API我想找到状态永远不再询问是否检查
这将很难。
您可以在shouldShowRequestPermissionRationale()
上致电ActivityCompat
。这将返回true
if:
您可以在checkSelfPermission()
上致电ContextCompat
。这将返回true
if:
您还可以跟踪自己之前是否曾向用户请求此权限,例如将该信息存储在SharedPreferences
中。
如果:
SharedPreferences
)和checkSelfPermission()
返回false
(因此用户未授予权限),shouldShowRequestPermissionRationale()
返回false
(因此您不应向用户提供理由)然后许可是在“不再问”状态。
答案 1 :(得分: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();
}
});
我想知道具体的许可已被拒绝,从不再问过。
如果用户选中从不再询问,您将在 onPermissionDisabled 上获得回电。
快乐编码:)