Since you're targeting API 10, the serial thread pool isn't available for your AsyncTask, but you can create your own. You can extend this to whatever AsyncTask features you may need...
First, create your thread pool that will execute things serially, a.k.a., a single thread.
public enum SerialTaskExecutor {
INSTANCE;
public static SerialThreadPool getInstance() {
return INSTANCE;
}
private final ExecutorService mExecutor;
private final Handler mMainHandler;
private SerialThreadPool() {
mExecutor = Executors.newSingleThreadExecutor();
mMainHandler = new Handler(Looper.getMainLooper());
}
public void executeTask(final SerialTask<P, R> t, final P... params){
if(t == null){
return;
}
mExecutor.execute(new Runnable(){
@Override
public void run(){
final R result = t.doInBackground(params);
mMainHandler.post(new Runnable(){
@Override
public void run(){
t.onMainThread(result);
}
});
}
});
}
}
and then define your serial task interface:
public interface SerialTask<P, R> {
public R doInBackground(P... params);
public void onMainThread(R result);
}
finally, instead of implementing AsyncTask, implement SerialTask:
public MyTextTask implements SerialTask<String, Void> {
@Override
public Void doInBackground(String... params) {
// do my string processing here
return null;
}
@Override
public void onMainThread(Void result)
// update ui here
}
}
Now your simple snippet becomes:
String text1="text1";
String text2="text2";
SerialTaskExecutor exe = SerialTaskExecutor.getInstance();
exe.executeTask(new MyTextTask(), text1);
exe.executeTask(new MyTextTask(), text2);