私は非常に遅れて投稿していることを知っていますが、詳細な例は誰かにとって役立つかもしれません.
私が気付いたのは、shouldShowRequestPermissionRationale() フラグを onRequestPermissionsResult() コールバック メソッドにチェックインすると、2 つの状態しか表示されないことです。
状態 1:-Return true:-- ユーザーが [アクセス許可を拒否] をクリックしたとき (初回を含む)。
状態 2:-false を返す:- ユーザーが「二度と質問しない」を選択した場合。
複数の許可リクエストの例を次に示します。
アプリは起動時に 2 つの権限を必要とします。SEND_SMS および ACCESS_FINE_LOCATION (どちらも manifest.xml に記載されています)。
アプリが起動するとすぐに、複数の権限をまとめて要求します。両方の権限が付与されている場合は、通常のフローになります。

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
}
}
private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
1 つ以上のアクセス許可が付与されていない場合、 activityCompat.requestPermissions() はアクセス許可を要求し、コントロールは onRequestPermissionsResult() コールバック メソッドに移動します。
onRequestPermissionsResult() コールバック メソッドの shouldShowRequestPermissionRationale() フラグの値を確認する必要があります。
2 つのケースのみがあります:--
ケース 1: - ユーザーが [アクセス許可を拒否] をクリックするたびに (初回を含む)、true が返されます。そのため、ユーザーが拒否した場合は、より多くの説明を表示して、再度質問を続けることができます。
ケース 2: -ユーザーが「二度と質問しない」を選択した場合のみ、false が返されます。この場合、制限された機能を続行し、ユーザーをガイドしてより多くの機能の設定からアクセス許可を有効にするか、アプリのアクセス許可が些細な場合はセットアップを終了できます。
ケース-1
CASE-2

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "sms & location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Log.d(TAG, "Some permissions are not granted ask again ");
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// // shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showDialogOK("SMS and Location Services Permission required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
.show();
// //proceed with logic by disabling the related features or quit the app.
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}