3

With android-annotations I can replace AsyncTask<Param, Progress, Result> with @UiThread.

But is possible to handle something like onPreExcecute / onPostExecute of AsynkTask?

gipinani
  • 14,038
  • 12
  • 56
  • 85

1 Answers1

6

There is no callback methods to handle this. But you can have the same result by calling methods at the beginning and at the end of your @Background annotated method (you said @UIThread but I hope this was a mistake :)).

Example :

@Background
void longRunningProcess() {
    longRunningProcessStarted();
    //...
    longRunningProcessEnded();
}

@UiThread
void longRunningProcessStarted() {

}

@UiThread
void longRunningProcessEnded() {

}

We also wrote a cookbook about this.

DayS
  • 1,561
  • 11
  • 15
  • I have read better your answer.. but why you say UiThread is a mistake? I have anotated a method with UiThread and this method create an excel file. I want to display a ProgressDialog that can cancel the operation in progress. – gipinani Dec 18 '13 at 14:23
  • 1
    It's a mistake because you shouldn't execute long-running process in UIThread, like file creation. – DayS Dec 18 '13 at 14:59
  • Ok! One last thing.. in order to kill the background service the only way is to search for the service by name and than kill it.. i have no way to keep an instance of it in the creating view? Thank you – gipinani Dec 18 '13 at 15:45
  • 1
    IF you want to cancel a background task (ie a `@Background` annotated method), the soon-to-be-release 3.0 version of AA will handle this use case. If you want to stop an Android service, you could just use the IntentBuilder in 3.0: `MyService_.intent(context).stop()` or before this version `context.stopService(MyService_.intent(context).get())` – DayS Dec 19 '13 at 11:53