如何防止在android中短时间连续按下按钮?

时间:2012-06-27 06:13:01

标签: android button

我收到的错误是用户在短时间内按下按钮太快。 以下是错误:

06-27 12:46:51.500: ERROR/ActivityManager(115): ANR in test.test.test (test.test.test/.testActivity)
06-27 12:46:51.500: ERROR/ActivityManager(115): Reason: keyDispatchingTimedOut
06-27 12:46:51.500: ERROR/ActivityManager(115): Load: 3.94 / 2.98 / 2.63
06-27 12:46:51.500: ERROR/ActivityManager(115): CPU usage from 5863ms to 0ms ago:
06-27 12:46:51.500: ERROR/ActivityManager(115):   5.6% 36/nandd: 0% user + 5.6% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):   1.7% 3/ksoftirqd/0: 0% user + 1.7% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):   1.5% 89/mediaserver: 1.5% user + 0% kernel / faults: 14 minor
06-27 12:46:51.500: ERROR/ActivityManager(115):   1% 5780/com.inno.wordcard: 1% user + 0% kernel / faults: 34 minor
06-27 12:46:51.500: ERROR/ActivityManager(115):   0.8% 115/system_server: 0.3% user + 0.5% kernel / faults: 8 minor
06-27 12:46:51.500: ERROR/ActivityManager(115):   0% 4933/kworker/0:0: 0% user + 0% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):   0.1% 176/com.android.systemui: 0.1% user + 0% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):   0.1% 1440/logcat: 0% user + 0.1% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115): 93% TOTAL: 2.9% user + 9% kernel + 81% iowait
06-27 12:46:51.500: ERROR/ActivityManager(115): CPU usage from 217ms to 736ms later:
06-27 12:46:51.500: ERROR/ActivityManager(115):   7.6% 36/nandd: 0% user + 7.6% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):   3.8% 115/system_server: 0% user + 3.8% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):     1.9% 116/HeapWorker: 0% user + 1.9% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115):     1.9% 149/InputDispatcher: 0% user + 1.9% kernel
06-27 12:46:51.500: ERROR/ActivityManager(115): 100% TOTAL: 0% user + 11% kernel + 88% iowait

我该如何防止这样的问题?

2 个答案:

答案 0 :(得分:2)

点按按钮后,您是否正在呼叫网络服务? 有几种选择:

1.添加一个进度对话框,在后台禁用任何内容。如果是Web服务,这是最好的解决方案。

2.禁用您执行任何操作的时间段内屏幕中的任何UI元素。

3.Spawn一个新线程或一个异步任务。

答案 1 :(得分:0)

您可以使用布尔变量来阻止在每次点击按钮时运行您的代码:

boolean isClicked=true;
Button btn = (Button) findViewById(YourId);
btn.setonClickListener(listener);
public onClickListener listener = new View.OnclickListener{
      onclick(View v){
      if(isClicked==true)
      {
        isClicked=false;
        doWork();
        //DO YOUR WORK HERE...
      }
      else
      {
        // DO NOTHING...
      }
      }
}
 public void doWork(){
      /// YOUR CODE HERE...

         //USE AsyncTask ,Hanlder,handlerThread, or Thread for performing long running         task in background on button click
        isClicked=true;


     }