Task with AsyncTask only runs once, then does not arrive on doInBackground

3

I'm facing a strange problem in my application, since I'm testing on 4 different devices but only two of them have the expected behavior.

Scenery / Devices:

  • Nexus 4 / Android 5.0 - Ok
  • Galaxy Tab 2 / Android 4.2.2 - Ok
  • LG Optimus / Android 4.4.2 - Error
  • Genesis / Android 4.1.2 - Error

As you can see, 4 different versions of Android and only the first two are working.

The problem in question is that the first time I install the application on the device, I run a task with AsyncTask and everything happens normally. But from there I can not run it any more.

As the onPreExecute() method still runs on the same thread, my debug arrives at it without problem, but not at doInBackground() .

I read something about the executeOnExecutor() method from API 11 (my application is from 15) but I do not think that's the case, since I do not want multiple tasks in parallel but a simple one.

The simple representation of my code is this:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected void onPreExecute() {
        // Preparo minha UI
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Executo minha tarefa
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Conclusão
        super.onPostExecute(result);
    }
}.execute();

Anyway, I do not know what can be happening, I've researched a lot and found nothing. My development environment is Android Studio 1.0.1 , compiled with version 21 of the SDK.

    
asked by anonymous 18.12.2014 / 17:34

2 answers

2

@PauloRodrigues I believe that because it is not "linked" Thread principal maybe the process is busy. One way to do this would be to "extend" AsyncTask into your main class.

Another thing I noticed is that you used return null; in a method that returns Void , I believe that this is not correct (or required) and you also used Void in a type of method, usually it would be "lowercase" (lower case):

protected void doInBackground(Void... params) {

The code should look like this:

public class AsyncTaskActivity extends Activity {
    ...

    public void onClick(View view){
            new meuMetodo().execute();
    }

    private class meuMetodo extends AsyncTask<Void, Void, Void> {
        ....
        @Override
        protected void doInBackground(Void... params) {
            // Executo minha tarefa
        }
        ....
    }
    ...
}
    
18.12.2014 / 18:23
0

Friend, try not to use the same object for each execution.

Each time you run AsyncTask, instantiate a new object.

I believe that this will work normally.

I've had similar issues and, researching, I've solved it that way.

Do not do this:

 AsyncTask obj = new AsyncTask();

    obj.execute();

Depois novamente ...


 obj.execute();

Whenever you run, do:

new AsyncTask().execute();

Considering the appropriate parameters for your case.

    
18.12.2014 / 18:21