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