0

有没有一种方法可以做到这一点而无需创建新的计时器和任务?我的代码基本上每 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;

                }
4

1 回答 1

0

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

但我认为它比使用计时器更脏;)

于 2012-07-20T08:21:12.297 回答