![](/img/trans.png)
[英]How can I request the permission dialog without the “Never ask again” option?
[英]How can I distinguish if the user request the permission for the first time or he clicked on the Never Ask Again button?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
} else {
if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
openCamera();
else {
//How can I know here if this is was the first time to request the permission or he pressed on Never Ask Again button before?
//Run this code if that was the first time to request the permission -> requestPermissionLauncher.launch(Manifest.permission.CAMERA);
//Run this code if he pressed on Never Ask Again button -> new AlertDialog(context)... etc - The dialog contains a button that moves the user to app settings to enable the permission
}
}
} else
openCamera();
我如何區分用戶是第一次請求權限還是單擊了“不再詢問”按鈕?
boolean
值可用於檢查用戶是否早些時候拒絕了權限。
將以下方法添加到上述文件或創建一個實用程序文件。
public static boolean neverAskAgainSelected(final Activity activity, final String permission) {
final boolean prevShouldShowStatus = getRatinaleDisplayStatus(activity,permission);
final boolean currShouldShowStatus = activity.shouldShowRequestPermissionRationale(permission);
return prevShouldShowStatus != currShouldShowStatus;
}
public static void setShouldShowStatus(final Context context, final String permission) {
SharedPreferences genPrefs = context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = genPrefs.edit();
editor.putBoolean(permission, true);
editor.commit();
} public static boolean getRatinaleDisplayStatus(final Context context, final String permission) {
SharedPreferences genPrefs = context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
return genPrefs.getBoolean(permission, false);
}
在權限請求處調用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
} else {
if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
openCamera();
else {
if(neverAskAgainSelected(this,Manifest.permission.CAMERA){
// new AlertDialog(context)...
}
else{
requestPermissionLauncher.launch(Manifest.permission.CAMERA);
}
}
}
} else{
openCamera();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.