You are re-implementing a lot of things that HandlerThread already does for
you. For example, the run() method is already implemented to drive the
message loop; you shouldn't replace that impl. If you want to do setup, you
do that in HandlerThread.onLooperPrepared().
Likewise, the getHandler() impl that does synchronization to wait for the
looper to be prepated is not needed -- just use HandlerThread.getLooper(),
which appropriately waits until the thread is running and the looper has
prepared.
Often you don't even need to subclass from HandlerThread at all. Just do:
HandlerThread handlerThread = new HandlerThread("My Handler");
Handler mBackgroundHandler = new MyHandler(handlerThread.getLooper());
Now you have mBackgroundHandler processing messages in the background, and
you can post messaged to it from any other thread.
Once you are done with the handler/thread, do handlerThread.quit().
On Wed, Apr 27, 2011 at 3:01 PM, Diego Tori
<[email protected]>wrote:
> 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
>
--
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