Running multiple AsyncTasks at the same time — not possible?

前端 未结 7 827
别那么骄傲
别那么骄傲 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:59

    This allows for parallel execution on all android versions with API 4+ (Android 1.6+):

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
    void startMyTask(AsyncTask asyncTask) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        else
            asyncTask.execute(params);
    }
    

    This is a summary of Arhimed's excellent answer.

    Please make sure you use API level 11 or higher as your project build target. In Eclipse, that is Project > Properties > Android > Project Build Target. This will not break backward compatibility to lower API levels. Don't worry, you will get Lint errors if your accidentally use features introduced later than minSdkVersion. If you really want to use features introduced later than minSdkVersion, you can suppress those errors using annotations, but in that case, you need take care about compatibility yourself. This is exactly what happened in the code snippet above.

    0 讨论(0)
提交回复
热议问题