Created
March 11, 2013 06:57
-
-
Save curioustechizen/5132347 to your computer and use it in GitHub Desktop.
Snippet demonstrating how to ensure AsyncTasks are always executed in parallel (the default pre-HC behavior)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.TargetApi; | |
import android.os.AsyncTask; | |
import android.os.Build; | |
public class AsyncTaskUtils { | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public static <P> void executeAsyncTaskInParallel( | |
AsyncTask<P, ?, ?> asyncTask, P... params) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); | |
} else { | |
asyncTask.execute(params); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Instead of myAsyncTaskInstance.execute(params), use the following: | |
AsyncTaskUtils.executeAsyncTaskInParallel(myAsyncTaskInstance, params); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment