如何显示AlertDialog作为权限理由

发布于 2025-01-31 03:16:24 字数 3527 浏览 1 评论 0原文

我有两个活动:main,以及runtimepermissionmanager,每当打开我的应用程序时,请首先启动。

当用户允许运行时许可时,该应用程序可以按预期工作,但是当他们拒绝它时,而不是arperdialog用权限理由显示出来的权限对话框,我只得到了一个无限Splash屏幕(因为我将其设置为在其父级活动的一生中始终放在屏幕上)。

我已经直接从RuntimePermissionsbasic与Android Studio捆绑在一起的示例直接复制了权限逻辑,只是用snackbararterdialog,所以我不' t看看为什么我的应用程序不起作用。

这是完整的RuntimePermissionManager参考活动。

谢谢。

public class RuntimePermissionManager extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
    private static final String TAG = "RuntimePermissionManager";
    private static final String PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    private static final int PERMISSION_ID = 0;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        final SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);
        // This ensures no hiccups while the splash screen is active.
        splashScreen.setKeepOnScreenCondition(() -> true);
        checkRuntimePermissionStatus();
        ThreadManager.unpackCriticalAssets(getApplicationContext());
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_ID) {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "Runtime permission has been granted.");
                startMainActivity();
            } else {
                Log.e(TAG, "Runtime permission has been denied.");
            }
        }
    }

    private void checkRuntimePermissionStatus() {
        if (ActivityCompat.checkSelfPermission(this, PERMISSION) == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Runtime permission has been granted.");
            startMainActivity();
        } else {
            requestRuntimePermission();
        }
    }

    private void requestRuntimePermission() {
        System.out.println(ActivityCompat.shouldShowRequestPermissionRationale(this, PERMISSION));
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, PERMISSION)) {
            System.out.println("HERE1");
            showPermissionRationaleDialog(R.string.permission_rationale_title, R.string.permission_rationale_message);
        } else {
            Log.e(TAG, "Runtime permission has been denied.");
            ActivityCompat.requestPermissions(this, new String[]{PERMISSION}, PERMISSION_ID);
        }
    }

    private void showPermissionRationaleDialog(final int title, final int message) {
        System.out.println("HERE2");
        AlertDialog.Builder builder = new AlertDialog.Builder(RuntimePermissionManager.this);
        builder.setTitle(title).setMessage(message).setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(RuntimePermissionManager.this, new String[]{PERMISSION}, PERMISSION_ID);
            }
        });
        builder.create().show();
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);
    }
}

I have two activities: a Main, as well a RuntimePermissionManager, which is started first whenever my app is opened.

The app works as expected when the user allows the runtime permission, but when they deny it, instead of the AlertDialog showing up with the permission rationale, the permission dialog is closed completely, and I just get an infinite splash screen (since I have set it to always be on screen during the lifetime of its parent activity).

I have copied the permission logic straight from the RuntimePermissionsBasic example bundled with Android Studio, and just replaced the Snackbar with an AlertDialog, so I don’t see why my app isn’t working.

Here’s the complete RuntimePermissionManager activity for reference.

Thanks.

public class RuntimePermissionManager extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
    private static final String TAG = "RuntimePermissionManager";
    private static final String PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    private static final int PERMISSION_ID = 0;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        final SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);
        // This ensures no hiccups while the splash screen is active.
        splashScreen.setKeepOnScreenCondition(() -> true);
        checkRuntimePermissionStatus();
        ThreadManager.unpackCriticalAssets(getApplicationContext());
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_ID) {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "Runtime permission has been granted.");
                startMainActivity();
            } else {
                Log.e(TAG, "Runtime permission has been denied.");
            }
        }
    }

    private void checkRuntimePermissionStatus() {
        if (ActivityCompat.checkSelfPermission(this, PERMISSION) == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Runtime permission has been granted.");
            startMainActivity();
        } else {
            requestRuntimePermission();
        }
    }

    private void requestRuntimePermission() {
        System.out.println(ActivityCompat.shouldShowRequestPermissionRationale(this, PERMISSION));
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, PERMISSION)) {
            System.out.println("HERE1");
            showPermissionRationaleDialog(R.string.permission_rationale_title, R.string.permission_rationale_message);
        } else {
            Log.e(TAG, "Runtime permission has been denied.");
            ActivityCompat.requestPermissions(this, new String[]{PERMISSION}, PERMISSION_ID);
        }
    }

    private void showPermissionRationaleDialog(final int title, final int message) {
        System.out.println("HERE2");
        AlertDialog.Builder builder = new AlertDialog.Builder(RuntimePermissionManager.this);
        builder.setTitle(title).setMessage(message).setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(RuntimePermissionManager.this, new String[]{PERMISSION}, PERMISSION_ID);
            }
        });
        builder.create().show();
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文