Um. Don't do that. AsyncTask and Looper are two fundamentally different threading models, which are not compatible. If you want a thread running a looper, use HandlerThread.
On Tue, Oct 27, 2009 at 10:53 AM, szabolcs <szabolcs.vr...@gmail.com> wrote: > > Hello, > > I ran into some trouble trying to use a Looper inside an AsyncTask > (its doInBackground method). Everything works fine until new Tasks are > being created by AsyncTask.sThreadFactory, but once I reach the > AsyncTask.CORE_POOL_SIZE limit and AsyncTask begins to recycle the > threads, sending messages to my looper (created inside doInBackground) > results in a MessageQueue RuntimeException: "sending message to a > Handler on a dead thread". > > The code I tried was: > > DaTask extends AsyncTask { > > // NOTE: there is only one DaTask being executed at any time, > // but DaTask is created/executed sequentially multiple times. > public static Looper mLooper; > > @Override > protected void onPreExecute() { > mHandler = new Handler() { > @Override > public void handleMessage(Message msg) { > ... > case MSG_EXIT: > DaTask.mLooper.quit(); > break; > }; > } > > @Override > protected Void doInBackground(Void... params) { > if (Looper.myLooper() == null) { > Looper.prepare(); > } > > mLooper = Looper.myLooper(); > > lm.requestLocationUpdates( > LocationManager.GPS_PROVIDER, 0, 0, > DaTask.this, mLooper > ); > > Looper.loop(); // process location updates > } > > The idea was to create a handler (the same way as the InternalHandler > () is created in the constructor of the AsyncTask) that is able to > stop a background process that performs work based on location > updates. > > As far as I can tell, the Looper that was registered as a ThreadLocal > during the first/successful run of DaTask is obtained properly even > during the second/unsuccessful run. However, it seems like it does not > process the messages. Why? Can a Looper not be 'restarted'? Looking at > the source of Looper.loop(), it seemes to me that it should be able > to. > > I am surely missing something here.. > > Thank you kindly for any suggestions, > > -szabolcs > > > > -- Dianne Hackborn Android framework engineer hack...@android.com 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---