Running multiple AsyncTasks at the same time — not possible?

前端 未结 7 828
别那么骄傲
别那么骄傲 2020-11-21 13:33

I\'m trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here\'s a simple snippet to describe my probl

7条回答
  •  悲&欢浪女
    2020-11-21 13:53

    Just to include the latest update (UPDATE 4) in @Arhimed 's immaculate answer in the very good summary of @sulai:

    void doTheTask(AsyncTask task) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Android 4.4 (API 19) and above
            // Parallel AsyncTasks are possible, with the thread-pool size dependent on device
            // hardware
            task.execute(params);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Android 3.0 to
            // Android 4.3
            // Parallel AsyncTasks are not possible unless using executeOnExecutor
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else { // Below Android 3.0
            // Parallel AsyncTasks are possible, with fixed thread-pool size
            task.execute(params);
        }
    }
    

提交回复
热议问题