Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:47:51+00:00 2026-06-08T06:47:51+00:00

Is there a way I can do this without having to create new timers

  • 0

Is there a way I can do this without having to create new timers and tasks? My code basically scans for wifi signals every 10 seconds. To ensure that the scan returns new results, I used another support class. Can someone verify that there aren’t any obvious errors with this as well?

//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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:47:52+00:00Added an answer on June 8, 2026 at 6:47 am

    Instead of using a Timer, create a boolean field named stop, and use a while(!stop) loop with Thread.sleep(1000) at the end of an iteration. Your processResults() method will do stop = true; and in your restart button’s listener do a stop=false;

    But I think it’s dirtier than use a Timer 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: There must be some way I can approach this without writing a whole
Is there any way to refactor this code so that it can omit unnecessary
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
Is there any way I can add this to my plugin? $this->_helper->viewRenderer->setNoRender();
Is there a way I can get this array walk with my anonymous function
I've recently come across this website. Now, is there any way I can actually
Is there a way to make this method generic so I can return a
I can't believe I'm asking this but, is there any way to get Chrome
I know there is a simple way to do this but I can't seem
Is there a way to add a resource to a ResourceDictionary from code without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.