如何在按下按钮之前停止运行代码(启用定时器)

时间:2012-07-20 07:38:04

标签: java android button timer

有没有办法我可以做到这一点,而无需创建新的计时器和任务?我的代码基本上每隔10秒扫描一次wifi信号。为确保扫描返回新结果,我使用了另一个支持类。有人可以验证这也没有任何明显的错误吗?

//Inside Button to start scanning

    final int DELAY = 10000;
        final Handler handler = new Handler();
        chkScan.setOnClickListener(new View.OnClickListener() {
        final Handler handler = new Handler();
        ReceiverWifi = new WifiReceiver();
        WIFI_Manager = new wifiScanner();
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        final Timer timer = new Timer();
        final TimerTask task = new TimerTask() {
         public void run() {
        handler.post(new Runnable() {
         public void run() {

       mainWifi.startScan();
     if ((WIFI_Manager.resultsAvailable())) {   
          processResults();                                            
            }
         // to ensure results come from latest scan
         // say there are no new results as of now   
            WIFI_Manager.waitForNextScan();

     }

            }
           );
            timer.schedule(task, 0, DELAY);

                }

           }
           });
        }

    public void processResults() {
    results = mainWifi.getScanResults();
    WIFI_Manager.pause() //stop getting wifi results
     //continue to process here
    //
    //



     //I wish to put a button here, but and wait for user input before continuing
    //but scans continue..
    chkLabel.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    }
     //more code
    // ...

    WIFI_Manager.resume();       
       }     




class WifiReceiver extends BroadcastReceiver {

                    @Override
                    public void onReceive(Context c, Intent intent) {

                        WIFI_Manager.getNewResults();

                    }

                }

                public class wifiScanner {

                    public wifiScanner() {

                        Pause = false;
                        new_results = false;

                    }

                    public boolean resultsAvailable() {

                        return new_results;
                    }

                    public void waitForNextScan() {

                        new_results = false;
                    }

                    public void getNewResults() {

                        new_results = true;

                    }

                    public boolean onPause() {

                        return Pause;

                    }

                    public void pause() {
                        unregisterReceiver(receiverWifi);
                        Pause = true;
                    }

                    public void resume() {
                        registerReceiver(receiverWifi, new IntentFilter(
                                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                        Pause = false;
                    }

                    // pause: false- access to scan results are allowed
                    // pause: true- cannot access scan results
                    private boolean Pause;

                    // new_results: false- no new WIFI_resuls
                    // new_results: true - there are new results to be processed
                    private boolean new_results;

                }

1 个答案:

答案 0 :(得分:0)

不是使用Timer,而是创建一个名为stop的布尔字段,并在迭代结束时使用while(!stop)循环和Thread.sleep(1000)。您的processResults()方法将执行stop = true;并在您的重启按钮的侦听器中执行stop=false;

但我觉得它比使用Timer更脏;)