问题
We need to implement a scenario where we have to close runtime permission dialog with Denial after 5 seconds of inactivity of user.
Steps:
User clicks the Save button
Permission dialog shown.
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