How to close runtime permission dialog programmatically?

本小妞迷上赌 提交于 2020-03-23 15:32:07

问题


We need to implement a scenario where we have to close runtime permission dialog with Denial after 5 seconds of inactivity of user.

Steps:

  1. User clicks the Save button

  2. Permission dialog shown.

  3. If user doesn't click accept/deny in 5 seconds

Expected Result: Dialog should be closed with deny as selected option.

Any help would be appreciated.

Also please let me know that if this is even possible to do so or not ?


回答1:


This is indeed a valid edge case, as explained below.

The solution

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

Why close the permission dialog?

Let's say the user starts your app and the permission dialog is immediately shown, e.g. from a fragment. Then the user switches to another app from which a new intent is sent to your app, e.g. Intent.ACTION_VIEW. Then you might want to change/rearrange the UI before showing the permission dialog:

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (isShowingPermissionDialog() {
        // Handle this intent after permisson dialog is dismissed!
        mPendingIntent = intent;
    } else {
        // This could interfere with the expected permission
        // callback of the active fragment etc.
        changeActiveFragment();
    }
}

Or, if you instead want to explicitly close the permission dialog:

private void closeSystemDialogs() {
    sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    closeSystemDialogs();

    changeActiveFragment();
}


来源:https://stackoverflow.com/questions/51102456/how-to-close-runtime-permission-dialog-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!