Sorry to bump the thread, but after creating a HandlerThread, all of the
locking stopped and the service stopped on a dime after unbinding even when
it waited for the allotted time before the next scan. Anyways, I was
wondering if I managed to implement it correctly:
class MyHandlerThread extends HandlerThread{
isRunning = false;
Handler _handler;
public MyHandlerThread(){
super(MyHandlerThread.class.getSimpleName(),
Process.THREAD_PRIORITY_BACKGROUND);
//register our custom BroadcastReceiver
//get Wi-Fi service
isRunning = true;
}
public void fireWhenReady(){
getHandler().sendEmptyMessage(DO_WIFI_SCAN);
}
public synchronized Handler getHandler() {
while (_handler == null) {
try {
wait();
} catch (InterruptedException e) {
//Ignore and try again.
}
}
return _handler;
}
@Override
public void run(){
Looper.prepare();
synchronized(this){
_handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DO_WIFI_SCAN:
//do our wifi scan call
break;
case SHUTDOWN:
removeMessages(DO_WIFI_SCAN);
getLooper().quit();
break;
}
}
};
notifyAll();
}
Looper.loop();
}
public void stopTheThread(){
isRunning = false;
//send SHUTDOWN message to handler
//unregister receiver and clean up
}
class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(isRunning){
//go through the results
//send the next wifi scan call to the handler after
sleeping for a certain amount of time
}
}
}
}
Also I wonder how I would incorporate a timeout mechanism like I tried in
previous attempts after calling the scan using this model?
--
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