我不知道爲什麼我的問題是downvoted,因爲這確實是Android版本之間的谷歌兼容性問題。
我用下面的輔助類來執行任務asynk:
public class AsyncTaskExecutionHelper {
static class HoneycombExecutionHelper {
public static <P> void execute(AsyncTask<P, ?, ?> asyncTask, boolean parallel, P... params) {
Executor executor = parallel ? AsyncTask.THREAD_POOL_EXECUTOR : AsyncTask.SERIAL_EXECUTOR;
asyncTask.executeOnExecutor(executor, params);
}
}
public static <P> void executeParallel(AsyncTask<P, ?, ?> asyncTask, P... params) {
execute(asyncTask, true, params);
}
public static <P> void executeSerial(AsyncTask<P, ?, ?> asyncTask, P... params) {
execute(asyncTask, false, params);
}
private static <P> void execute(AsyncTask<P, ?, ?> asyncTask, boolean parallel, P... params) {
if (Build.VERSION.SDK_INT >= 11) {
HoneycombExecutionHelper.execute(asyncTask, parallel, params);
} else {
asyncTask.execute(params);
}
}
}
打電話給你asynk任務以下列方式:
CustomAsynkTas customTask = new CustomAsynkTask();
AsyncTaskExecutionHelper.executeParallel(customTask);
其中
CustomAsynkTask extends AsyncTask
{
//etc
}
經過測試,它適用於所有Android版本。
THREAD_POOL_EXECUTOR在3.0中首次引入時,它如何在所有android版本上運行? –