简体   繁体   English

通过单击按钮两次足够快来绕过模式Android对话框?

[英]Bypass modal Android dialogs by clicking buttons twice fast enough?

Let's say we have two buttons, each with a OnClickListener. 假设我们有两个按钮,每个按钮都有一个OnClickListener。 Each of listeners show a ProgressDialog and do some background work. 每个侦听器都显示一个ProgressDialog并进行一些后台工作。 (Behind the scene is an AsyncTask, the dialog is opened in onPreExecute. I don't think it matters, just for the record...). (在后台是一个AsyncTask,该对话框在onPreExecute中打开。我认为这并不重要,仅出于记录目的...)。 Let's say there is some rule saying no more than one background worker may be active at any given time. 假设有一条规则说,在任何给定时间,最多只能有一个后台工作者处于活动状态。

My assumption was that the Dialog prevents two background workers running at the same time. 我的假设是对话框阻止两个后台工作程序同时运行。 I thought the modal dialog blocks the UI and it's not possible to click another button after the show() method of the dialog is called. 我认为模式对话框会阻止UI,并且在调用对话框的show()方法后无法单击另一个按钮。 I was wrong. 我错了。

If you click the buttons fast enough, it's possible to trigger both background workers (almost) at the same time. 如果您足够快地单击按钮,则有可能(几乎)同时触发两个后台工作人员。 The log shows that it's possible to click two Buttons within a 150 ms time span despite the Dialog: 日志显示,尽管有对话框,也可以在150毫秒的时间内单击两个按钮:

04-14 18:34:04.390: DEBUG/greenrobot(1860): Clicked: 2131034112
04-14 18:34:04.470: DEBUG/greenrobot(1860): doInBackground2: 2131034112
04-14 18:34:04.540: DEBUG/greenrobot(1860): Clicked: 2131034113
04-14 18:34:04.570: DEBUG/greenrobot(1860): doInBackground2: 2131034113

The dialog code looks like this: 对话框代码如下所示:

progressDialog = new ProgressDialog(currentActivity);
progressDialog.setMessage(msg);
progressDialog.show(); 

What did I miss? 我错过了什么? I hope I missed something really stupid, because if not, I cannot think of a nice and solution preventing UI interaction after the click. 我希望我错过了一些真正愚蠢的东西,因为如果没有,我想不出一个很好的解决方案来防止单击后的UI交互。 Synchronizing the background workers is not a solution because the UI and scenario is more complex. 同步后台工作程序不是解决方案,因为UI和方案更加复杂。

Disable the button after it is clicked, until it is safe to be clicked again. 单击按钮后将其禁用,直到可以再次安全单击为止。

show() is asynchronous. show()是异步的。 The dialog will not appear immediately upon the call to show() . 调用show()时,该对话框不会立即出现。 It will occur moments later. 稍后会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM