我正在开发一个辅助功能应用.当用户想要离开应用程序时,我会显示一个对话框,他必须确认他要离开,如果他在5秒后没有确认对话框应该自动关闭(因为用户可能会意外打开它).这与更改屏幕分辨率时在Windows上发生的情况类似(出现警报,如果您不确认,则会恢复到之前的配置).
这是我显示对话框的方式:
AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
dialog.create().show();
Run Code Online (Sandbox Code Playgroud)
如何在显示后5秒钟关闭对话框?