Asif Banaras Dhamial
Asif Banaras Dhamial

Reputation: 75

How to close runtime permission dialog programmatically?

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 ?

Upvotes: 2

Views: 1881

Answers (1)

l33t
l33t

Reputation: 19966

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();
}

Upvotes: 1

Related Questions