I suggest not using a raw thread.  If you need it on a separate thread, use
a HandlerThread and Handler.sendMessageDelayed() to do your timing.

Even better -- if the code is not doing blocking operations, there is no
need for another thread at all; just make a class with a Handler in it and
use Handler.sendMessageDelayed() to schedule your work on the main thread.

On Mon, Apr 18, 2011 at 3:58 PM, Diego Tori
<[email protected]>wrote:

> So yeah, I managed to try calling the wifi scanner in this fashion within
> my thread:
>         private Object _lock = new Object();
>
>         private final long SLEEP_TIME = 15000; //Scan every 15 secs
>         private final long WIFI_SCAN_TIMEOUT = 60000; //One minute timeout
> for getting called back, otherwise, initiate a new scan.
>
>
>        @Override
>         public void run() {
>             while(running){
>                 //Start a new scan;
>                 wifiSearchComplete = false;
>                 _wifiMan.startScan();
>                 while(!wifiSearchComplete){
>                     synchronized(_lock){
>                         try{
>                             _lock.wait(WIFI_SCAN_TIMEOUT);
>                         } catch (InterruptedException ie){
>                              Log.d(TAG, TAG+".run() caught " +
> ie.getMessage() +" when trying to sleep for " + (WIFI_SCAN_TIMEOUT/1000)
> +"secs.");
>                              Thread.currentThread().interrupt();
>                         }
>                     }
>                     if(!wifiSearchComplete){
>                         synchronized(_lock){
>                             //Try scanning again since we didn't get called
> back at all;
>                             _lock.notify();
>                         }
>                     }
>                 }
>             }
>         }
>
>         public boolean isRunning(){
>             return running;
>         }
>
>         public void stop(){
>             synchronized(_lock){
>                 running = false;
> //unregister receivers and cleanup
>                 _lock.notify();
>
>             }
>         }
>
>         @Override
>         public void onReceive(Context context, Intent intent) {
>             synchronized(_lock){
>                    wifiSearchComplete = true;
>                    //iterate through our SSID's
>                     try{
>                         _lock.wait(SLEEP_TIME);
>                     } catch (InterruptedException ie){
>                          Log.d(TAG, TAG+".onReceive() caught " +
> ie.getMessage() +" when trying to sleep for " + (SLEEP_TIME/1000) +"secs.");
>                          Thread.currentThread().interrupt();
>                     }
>                     _lock.notify();
>                 }
>         }
>
> However, even though it waits every 15 seconds before it scans again, when
> trying to exit my test activity (calling onDestroy) it blocks the main
> thread for the sleep time, before it unbinds the service. In other words, is
> this the appropriate way of trying to accomplish what I want to do w/o
> blocking, or do I have to simply create a BroadcastReceiver and call
> Thread.sleep at the end of onReceive before calling starting a new scan?
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to