0

Currently building an Android app and checking it on Genymotion running 4.1.1. I'm using AsyncTask to call the Bing Translate API to translate from text:

class TranslateFacebookText extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... message) {
        String translatedText = "";
        try {
            translatedText = Translate.execute(message[0], Language.AUTO_DETECT, Language.ENGLISH);
        } catch (Exception e) {
            ....
        }
        return translatedText;  
    }

    @Override
    protected void onPostExecute(String translatedText) {
        message = translatedText;
        confirmTTSData();
    }
}

public void onClick(View src) {
    TranslateFacebookText translateTask = new TranslateFacebookText();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        translateTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, message);
    }
    else {
        translateTask.execute(message);
    }
}

I'm using this method to start the task after reading this question: Android SDK AsyncTask doInBackground not running (subclass)

I'm doing so, since after about 2-5 minutes from the programs start, the AsyncTask refuses to run. That is, doInBackground does not get called, nor does onPostExecute. the onClick DOES get called, creates the new AsyncTask and runs the execution code, but the doInBackground does not get called.

This is completely random. I'm not doing anything else - just waiting there for a couple of minutes, and afterwards clicking the button again to see this happen. This is also true with a service which runs every specified time using a Handler and postDelayed. Here's an example:

public class MyService extends Service {
    private Handler periodicEventHandler;
    private final int PERIODIC_EVENT_TIMEOUT = 600000;

    @Override
    public void onCreate() {
        periodicEventHandler = new Handler();
        periodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
    }

    private Runnable doPeriodicTask = new Runnable()
    {
        public void run()
        {
            TranslateFacebookText translateTask = new TranslateFacebookText();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                translateTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, message);
            }
            else {
                translateTask.execute(message);
            }
            periodicEventHandler.postDelayed(doPeriodicTask, PERIODIC_EVENT_TIMEOUT);
        }
    };

    class TranslateFacebookText extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... message) {
            String translatedText = "";
            try {
                translatedText = Translate.execute(message[0], Language.AUTO_DETECT, Language.ENGLISH);
            } catch (Exception e) {
                ....
            }
            return translatedText;  
        }

        @Override
        protected void onPostExecute(String translatedText) {
            message = translatedText;
            confirmTTSData();
        }
    }
}

The doPeriodicTask runs fine, again creating the AsyncTask and calling the execution code, but doInBackground never gets called. If I change PERIODIC_EVENT_TIMEOUT to 8000, for example, doInBackground would get called fine.

Ideas?

Community
  • 1
  • 1
jbkkd
  • 1,510
  • 5
  • 18
  • 37
  • Is your `onClick()` inside your `AsyncTask` class and have you debugged to see if the `onClick()` is getting called? – codeMagic Nov 14 '13 at 23:44
  • If your performing a long running task maybe you should put it into a normal thread – James andresakis Nov 14 '13 at 23:47
  • @codeMagic - `onClick()` is not inside the `AsyncTask` class, it's in the `Activity` which contains the `AsyncTask` class. And yes, it's being called – jbkkd Nov 15 '13 at 09:33
  • @Jamesandresakis - what would qualify as long running task? and what are the advantages of a thread in this case compared to and `AsyncTask`? – jbkkd Nov 15 '13 at 09:34
  • Don't forget you can use Log.d(TAG, message) to help you debug code by outputting to logcat when ever your program reaches a certain point. Also how is onClick attached to the button? – MungoRae Nov 15 '13 at 10:27
  • @MungoRae - it's a simple `setOnClickListener`. I may have not emphasised this enough - the `onclick()` gets called, creates a new `translateTask` and runs the execution code, but `doInBackground` does not get called. – jbkkd Nov 15 '13 at 11:34
  • A long running task is anything that you may need to run constantly. An asynctask automatically runs and then stops and is thrown into a threadpool automatically and the system handles it. if your task gets ran multiple times it could be getting thrown on a thread in the pool thats waiting for others to finish. – James andresakis Nov 15 '13 at 19:47

0 Answers0