3

I have several AsyncTasks doing network operations. I was recently asked to add, after each of these operations, another network call that invokes a remote service sending some statistics. I'd like this statistics call not to delay 'main' network calls, and so I was thinking of executing them in a thread created and started inside the AsyncTask's doInBackground(). This thread would most probably end after the doInBackground() and possibly the whole AsyncTask has ended. I tried this and it works, but I was wondering if there are side effects, such as memory leaks or similar?

Thanks ;)

Venator85
  • 10,245
  • 7
  • 42
  • 57

2 Answers2

0

If you want to start thread in doInBackground method, you can start it in onProgressUpdate() method

Example:

protected class MyTask extends AsyncTask<Void,Integer,Void> {

        public static final int START_THREAD = -1;
        @Override
        protected void onProgressUpdate(Integer... values) {
            if(values[0] == START_THREAD){
                startThread();
            }
        }

    @Override
    protected Void doInBackground(Void... params) {
        publishProgress(START_THREAD);
        return null;
    }
}
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Rajesh Batth
  • 1,672
  • 2
  • 19
  • 23
0

Try starting a second AsyncTask in the first AsyncTasks 'onPostExecute' Method. For me this worked without any issues.

Thommy
  • 5,070
  • 2
  • 28
  • 51
  • Mmmh that would require some effort to make the data I need to send for statistics available in the `onPostExecute()` too... Do you see any problem with my approach which, by the way, is working? – Venator85 Sep 30 '11 at 08:48