-
Notifications
You must be signed in to change notification settings - Fork 0
WorkingWithThreads
Since AndroidAnnotations 1.0
Get rid of AsyncTasks!!
The @Background annotation indicates that a method will run in a thread other than the ui thread.
Usage example:
void myMethod() {
someBackgroundWork("hello", 42);
}
@Background
void someBackgroundWork(String aParam, long anotherParam) {
[...]
}The method is executed on a separate thread, but this doesn't necessarily mean that a new thread will be started, because we use a shared cached thread pool executor (which can be replaced) to prevent creating too much threads.
This also means that two
@Backgroundmethods may run in parallel
Since AndroidAnnotations 3.0
If you want to cancel a background task, you can use the id field. Every task could then be cancelled by BackgroundExecutor.cancelAll("id");:
void myMethod() {
someCancellableBackground("hello", 42);
[...]
boolean mayInterruptIfRunning = true;
BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);
}
@Background(id="cancellable_task")
void someCancellableBackground(String aParam, long anotherParam) {
[...]
}Since AndroidAnnotations 3.0
By default, @Background method are processed in parallel. If you want them to be executed sequentially, you can use the serial field. All background tasks with the same serial will be executed sequentially:
void myMethod() {
for (int i = 0; i < 10; i++)
someSequentialBackgroundMethod(i);
}
@Background(serial = "test")
void someSequentialBackgroundMethod(int i) {
SystemClock.sleep(new Random().nextInt(2000)+1000);
Log.d("AA", "value : " + i);
}Since AndroidAnnotations 3.0
If you need to add a delay before a background method is run, you can use the delay parameter:
@Background(delay=2000)
void doInBackgroundAfterTwoSeconds() {
}The @UiThread annotation indicates that a method will run in the ui thread.
Usage example:
void myMethod() {
doInUiThread("hello", 42);
}
@UiThread
void doInUiThread(String aParam, long anotherParam) {
[...]
}No more AsyncTask<Param, Progress, Result>!!
If you need to add a delay before a method is run on the UI Thread, you can use the delay parameter:
@UiThread(delay=2000)
void doInUiThreadAfterTwoSeconds() {
}AndroidAnnotations was created by Pierre-Yves Ricau and is sponsored by eBusinessInformations.
04/03/2013 The 2.7.1 release is out
- Get started!
- Download
- Cookbook, full of recipes
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow