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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---